Simple Program Problem

Hello all, I am getting no errors in this program when I check it in visual studios, yet when I try to run it, the 2nd option (2. Calculate the area of a rectangle) does not properly work. Does anyone know what I am doing wrong?

thanks,
*program not completely finished

# include <iostream>
# include <cmath>
using namespace std;
int main () {

int num;
double area,area_r,area_t;
double radius,length,width,triangle_b,triangle_h;
cout << "Geomety Calculator" << endl;
cout << "1. Calculate the area of a circle"<< endl;
cout << "2. Calculate the area of a rectangle"<< endl;
cout << "3. Calculate the area of a triangle"<< endl;
cout << "4. Quit"<< endl;
cin >> num;

//circle
if(num == 1)
cout << "Please enter radius:";
cin >> radius;
if (radius > 0)
area = 3.14159*pow(radius,2);
cout << "The area is " << area << endl;
if (radius < 0)
cout << "Invalid";

//rectangle
if(num == 2)
cout <<"Please enter length:";
cin >> length;
cout <<"Please enter width:";
cin >> width;
area_r = length * width;
cout << "The area is "<< area_r << endl;

//triangle
if (num == 3)
cout <<"Please enter traingle base:";
cin >> triangle_b;
cout <<"Please enter triangle height:";
cin >> triangle_h;
area_t = triangle_b*triangle_h;
cout <<"The area is "<< area_t << endl;






return 0;
}
you forgot the {} brackets...

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
//circle
if(num == 1){//<--open curly brace here
    cout << "Please enter radius:"; 
    cin >> radius;
    if (radius < 0)
       cout << "Invalid";
    else if (radius > 0)
       area = 3.14159*pow(radius,2);
    cout << "The area is " << area << endl;
    //how about radius == 0?
}//close curly brace
//rectangle	
else if(num == 2){
    cout <<"Please enter length:";
    cin >> length;
    cout <<"Please enter width:";
    cin >> width; 
    area_r = length * width;
    cout << "The area is "<< area_r << endl;
}
//triangle
else if (num == 3){
    cout <<"Please enter traingle base:";
    cin >> triangle_b;
    cout <<"Please enter triangle height:";
    cin >> triangle_h;
    area_t = triangle_b*triangle_h;
    cout <<"The area is "<< area_t << endl;
}


but I prefer you to use switch

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
35
36

switch(num){
//circle
case 1:
    cout << "Please enter radius:"; 
    cin >> radius;
    if (radius < 0)
       cout << "Invalid";
    else if (radius > 0)
       area = 3.14159*pow(radius,2);
    cout << "The area is " << area << endl;
    //how about radius == 0?
    break;
//rectangle	
case 2:
    cout <<"Please enter length:";
    cin >> length;
    cout <<"Please enter width:";
    cin >> width; 
    area_r = length * width;
    cout << "The area is "<< area_r << endl;
    break;
//triangle
case 3:
    cout <<"Please enter traingle base:";
    cin >> triangle_b;
    cout <<"Please enter triangle height:";
    cin >> triangle_h;
    area_t = triangle_b*triangle_h;
    cout <<"The area is "<< area_t << endl;
    break;
case 4:
    break;
default:
    cout<<"invalid input";
}
Last edited on
Ahhh I see, thanks alot man, appreciate the help
Topic archived. No new replies allowed.