In my geometry calculator, I cannot get the the second or 3rd "If" statements to correspond after entering the correct number. Does anyone know what the problem is?
It claims no errors, but only when I enter "1" does the program correspond correctly. When I enter 2 it malfunctions
# include <iostream>
# include <cmath>
using namespace std;
int num;
int main () {
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;
cout << "Enter your choice"<< 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 = 0.5*triangle_b*triangle_h;
cout <<"The area is "<< area_t << endl;
If your if statement lasts for more than 1 line, you need to put the whole part of the if statement in brackets( { } ); other wise, it just "ifs" one line.