-
Code check again please
Hmm Im trying to make a program that calculates the area of a rectangle and after you get the first rectangle, you can type in C to continue or anything else to stop. It doesnt work though.
Code:
#include <iostream.h>
#include <conio.h>
int main()
{
char cont=c
double length;
double height;
for(i=0;cont=c;i++)
{
cout<<"This will calculate the area of a rectangle for you.\n"<<endl;
cout<<"Enter the length of the rectangle: "<<endl;
cin>>length;
cout<<"Enter the height of the rectangle: "<<endl;
cin>>height;
cout<<"\n The area of the rectangle is: "<<(length*height)<<endl;
cout<<"Type in C to continue or anything else to close the program."<<endl;
cin>>cont;
}
getch();
getch();
return 0;
}
-
I tried this...
PHP Code:
#include <iostream.h>
#include <conio.h>
#include <cstdlib>
int main(int argc, char* argv[])
{
docheck();
return 0;
}
int docheck()
{
char cont;
double length;
double height;
system("cls");
cout<<"This will calculate the area of a rectangle for you.\n"<<endl;
cout<<"Enter the length of the rectangle: "<<endl;
cin>>length;
cout<<"Enter the height of the rectangle: "<<endl;
cin>>height;
cout<<"\n The area of the rectangle is: "<<(length*height)<<endl;
cout<<"Type in C to continue or anything else to close the program."<<endl;
cin>>cont;
if(cont == 'C' || cont == 'c')
{
docheck();
}
getch();
//return 0;
}
I am also pretty much a newb to C++. When I execute this code, I get "undeclared identifier pointing to the docheck(); in main, and a "redefinition; different type modifiers" pointing to the sub docheck(); itself. Help?
-
AnT: you need a function prototype. Put
int docheck();
before the main function and you'll be allright.
Aerials: just a few errors:
Code:
#include <iostream> // the standard changed: you should use this
#include <conio.h>
using namespace std; // this is a resulting necessity
int main()
{
char cont='c'; // char literals are to be wrapped in single quotes
double length;
double height;
while(cont == 'c') // better to use a while here, comparison is ==
{
cout<<"This will calculate the area of a rectangle for you.\n"<<endl;
cout<<"Enter the length of the rectangle: "<<endl;
cin>>length;
cout<<"Enter the height of the rectangle: "<<endl;
cin>>height;
cout<<"\n The area of the rectangle is: "<<(length*height)<<endl;
cout<<"Type in C to continue or anything else to close the program."<<endl;
cin>>cont;
}
getch();
getch();
return 0;
}
-
You guys might want to use a do while loop, I not at my workstation, but I 'll do an example with a loop when I get home later.