I am trying to write a small program that calculates that area and perimeter of a rectangle. The problem I am having is the program needs to output an error message if the area of the rectangle is a negative integer. I am trying to use an IF statement but can't get the program to compile correctly
#include <iostream>
#include <cstdlib>
using namespace std;
main(){
int length, width;
int perimeter, area;
cout << "Length = "; // prompt
cin >> length; // enter length
cout << "Width = "; // prompt
cin >> width; // enter width
perimeter = 2*(length+width); // perimeter
area = length*width; // area
cout << endl
<< "Perimeter is " << perimeter;
cout << endl;
if (area <0){
cout << "Error Postive Intergers Only";
}
<< "Area is " << area
<< endl;
system("PAUSE");
return 0; // results
}
Please put your code in the code tags next time. What errors are you getting? I can see that where you have "Area is" needs cout or some other object and you are also missing a semicolon.
I got the above code to run, now I realize I need the IF statements to output error messages if negative width or length is entered. The compiler says: line 6 forbids declaration of main with no type...
lines 21,24, and 31 says error expected primary-expression before "<" token
Your code compiles, but it doesn't "work." Checking for negative length/width should be done in a recursion loop, like a do {...}while (...); so that if a bad number is put in, then the code will allow the user to try again.