Having Trouble with my program

Here is my program, I am having trouble with it. Whenever you type in 4.5 as an option, it should restate the menu and have you enter in a valid option. The only problem is, after the menu comes back up, I type in a valid option and it closes....


#include <iostream>
#include <windows.h>
#include <cmath>

int main()
{
using namespace std;

float option;
double radius;
double AC;
double length;
double width;
double AR;
double height;
double base;
double AT;

cout<<"This Program is designed to calculate the area of various shapes" <<endl;
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"**********Options**********" <<endl;
cout<<"1 Circle" <<endl;
cout<<"2 Rectangle" <<endl;
cout<<"3 Triangle" <<endl;
cout<<"4 Quit" <<endl;
cout<<"***************************" <<endl;
cout<<"Which would you like to select?" <<endl;
cin>>option;

while (option==1)
{
cout<<"What is the radius of your circle?" <<endl;
cin>>radius;
AC=radius*radius*3.14159265;
cout<<AC;
cout<<" is the area of your circle" <<endl;
cout<<"" <<endl;
system("Pause");
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"**********Options**********" <<endl;
cout<<"1 Circle" <<endl;
cout<<"2 Rectangle" <<endl;
cout<<"3 Triangle" <<endl;
cout<<"4 Quit" <<endl;
cout<<"***************************" <<endl;
cout<<"Which would you like to select?" <<endl;
cin>>option;
}
while (option==2)
{
cout<<"What is the Lenght of your Rectangle?" <<endl;
cin>>length;
cout<<"What is the Width of your Rectangle?" <<endl;
cin>>width;
AR=length*width;
cout<<AR;
cout<<" is the area of your rectangle" <<endl;
cout<<"" <<endl;
system("Pause");
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"**********Options**********" <<endl;
cout<<"1 Circle" <<endl;
cout<<"2 Rectangle" <<endl;
cout<<"3 Triangle" <<endl;
cout<<"4 Quit" <<endl;
cout<<"***************************" <<endl;
cout<<"Which would you like to select?" <<endl;
cin>>option;
}
while (option==3)
{
cout<<"What is the height of your Triangle?" <<endl;
cin>>height;
cout<<"What is the base of your Triangle?" <<endl;
cin>>base;
AT=0.5*base*height;
cout<<AT;
cout<<" is the area of your triangle"<<endl;
cout<<"" <<endl;
system("Pause");
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"**********Options**********" <<endl;
cout<<"1 Circle" <<endl;
cout<<"2 Rectangle" <<endl;
cout<<"3 Triangle" <<endl;
cout<<"4 Quit" <<endl;
cout<<"***************************" <<endl;
cout<<"Which would you like to select?" <<endl;
cin>>option;
}
while (option==4)
{
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"Thank You and Goodbye";
Sleep(2000);
return 0;
}
while (option>4)
{
cout<<"" <<endl;
cout<<"Please Select a Valid Option" <<endl;
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"**********Options**********" <<endl;
cout<<"1 Circle" <<endl;
cout<<"2 Rectangle" <<endl;
cout<<"3 Triangle" <<endl;
cout<<"4 Quit" <<endl;
cout<<"***************************" <<endl;
cout<<"Which would you like to select?" <<endl;
cin>>option;
}
while (option <=0)
{
cout<<"" <<endl;
cout<<"Please Select a Valid Option" <<endl;
cout<<"" <<endl;
cout<<"" <<endl;
cout<<"**********Options**********" <<endl;
cout<<"1 Circle" <<endl;
cout<<"2 Rectangle" <<endl;
cout<<"3 Triangle" <<endl;
cout<<"4 Quit" <<endl;
cout<<"***************************" <<endl;
cout<<"Which would you like to select?" <<endl;
cin>>option;
}
}
You have coded it in bad way.

You should done something like this -

1
2
3
4
5
6
7
8
while(option!=4) // check if user quits
{
switch(option){
case 1: calCircle(); // function for calculating circle
case 2:calRectangle();  // function for calculating rectangle
case3:calTriangle(); // function for calculating triangle
}
}
Last edited on
If the person types in 4.5 then your program will read in the 4 because you asked it for an int. 4 is a legal int so it will run with that. The remaining .5 will still be there for when you ask for more input.

Oh, my mistake, you ask for a float :)
Last edited on
I think the problem is your if()s:
 
if (option==4) // comparing float with int 

The problem with that is that you are comparing your float with an int. In order to compare it with a float you need to add .0f to the number:
 
if (option==4.0f) // now comparing float with float 

Why is option a float? Wouldn't things be much better if it was an int?
What a mess.
You should rewrite and resubmit your code in line with Cody39e (and others) suggestions because the improvement will be significant. Also find an alternative for system ("pause") to keep the window open.
Topic archived. No new replies allowed.