Decision Maker

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.
Missing cout.

1
2
3
4
5
6
7
<< "Area is " << area
<< endl;

// should be

cout << "Area is " << area << endl;
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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;
  cout << "Perimeter is " << perimeter;
  cout << endl;

  if (area =<0){
    cout << " Error Postive Intergers Only ";

  if (length =<0)
      cout << " Error Postive Length Only ";

  if (width <0)
      cout << " Error Postive Width Only ";
      cout  << "Area is " << area;
  }
       << endl;
       system("PAUSE");
    return 0;                  // results
}
I fixed lines 6 and 31
Last edited on
You're missing closing braces for a bunch of the if statements and the endl needs a cout before it.
This code works :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdlib>

using namespace std;

  int main(){
  int length, width;
  int perimeter, area;


  cout <<  "Length = ";             //  prompt
  cin >> length;                    //  length
  cout << "Width = ";               //  prompt
  cin >> width;                     //  width
  perimeter = 2*(length+width);     //  perimeter
  area = length*width;              //  area
  cout << endl;
  cout << " Perimeter is " << perimeter;
  cout  << " Area is " << area;
  cout << endl;

  if (length <0){
  length = length;
  cout << " Error Postive Length Only ";
  }

  if (width <0){
  width = width;
  cout << " Error Postive Width Only ";
  }

  system("PAUSE");
  return 0;
}
Last edited on
cokane,

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.
Topic archived. No new replies allowed.