Hello! I have written a program that will calculate the area of a circle, rectangle, and a triangle if so chosen by the integers the user inputs (1-4, 4 is quit). Inside of these functions there is a similar menu asking if you would like to quit the program, go back to the main menu, or get another area from the current object (circle, rectangle, or triangle). However, when I enter 1 to quit, it loops the function again, making me have to enter another base/height/radius to move on. Once I have finished this and the menu asking me what I would like to do next pops up (quit, go back to the main menu, or get another area) I press 1 again and it finally exits. The same thing happens when I try to go back to the main menu except it loops the function 5 times (each time I need to have the input of 2 for the menu that pops up). I have been trying to figure out why it takes so many inputs to get out of the functions nOptionOne(), nOptionTwo(), and nOptionThree(), to no avail. Can anybody tell me what is wrong in my code?
Thanks in advance!
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#define PI 3.141592653589793238462643383279502884197169399375
usingnamespace std;
//TRY USING THE NEW WORD TO SOLVE YOUR PROBLEM!!!!!!!!!!!
int main()
{
void displayProgramExplanation();
int nOptionOne();
int nOptionTwo();
int nOptionThree();
int nLoopContCon , nLoopContCon2;
int nLoopContCon3 = 0;
do
{
displayProgramExplanation();
cout << "1. Calculate the Area of Circle.\n"
<< "2. Calculate the Area of Rectangle.\n"
<< "3. Calculate the Area of Triangle.\n"
<< "4. Quit.\n"
<< "Enter your choice:" << endl;
int nOption = 0;
cin >> nOption;
switch(nOption)
{
case 1:
nOptionOne();
if(nOptionOne() == 0)
{
system("PAUSE");
return 0;
}
elseif(nOptionOne() == -1)
{
cout << "An error has occured within the program.\n"
<< "The program will now exit. Please retry executing the program.";
return 0;
}
elseif (nOptionOne() == 2)
{
nLoopContCon = nOptionOne();
}
break;
case 2:
nOptionTwo();
if(nOptionTwo() == 0)
{
system("PAUSE");
return 0;
}
elseif(nOptionTwo() == -1)
{
cout << "An error has occured within the program.\n"
<< "The program will now exit. Please retry executing the program.";
return 0;
}
elseif (nOptionTwo() == 2)
{
nLoopContCon2 = nOptionTwo();
}
break;
case 3:
nOptionThree();
if(nOptionThree() == 0)
{
system("PAUSE");
return 0;
}
elseif(nOptionThree() == -1)
{
cout << "An error has occured within the program.\n"
<< "The program will now exit. Please retry executing the program.";
return 0;
}
elseif (nOptionThree() == 2)
{
nLoopContCon3 = nOptionThree();
}
break;
case 4:
system("PAUSE");
return 0;
}
} while(nLoopContCon == 2 || nLoopContCon2 == 2 || nLoopContCon3 == 2);
}
void displayProgramExplanation()
{
cout << "This program calculates four things; the area of a circle, the area of a rectangle,\n and the area of a triangle."
<< "These options are displayed below. Select 1-4 to choose what you would like to do." << endl;
}
int nOptionOne()
{
int nOptionSelect;
do
{
system("CLS");
cout << "Please enter the radius of the circle:";
double dRadius = 0.0;
cin >> dRadius;
double dCArea = PI * (dRadius*dRadius);
cout << "The area of the circle is " << dCArea << endl;
do{
cout << "What would you like to do? Choose 1-3.\n"
<< "1. Quit?\n"
<< "2. Go back to the menu?\n"
<< "3. Find another circle's area?";
cin >> nOptionSelect;
switch(nOptionSelect)
{
case 1:
return 0;
break;
case 2:
return 2;
break;
case 3:
break;
default:
cout << "You didn't enter a valid number. Try again.";
system("PAUSE");
break;
system("CLS");
}
}while(nOptionSelect <= 0 && nOptionSelect > 3);
}while(nOptionSelect == 3);
return -1;
}
int nOptionTwo()
{
int nSecondOS;
do{
system("CLS");
cout << "What is the base of the rectangle?\n";
double BoR = 0.0;
cin >> BoR;
cout << "What is the hieght of the rectangle?\n";
double HoR = 0.0;
cin >> HoR;
double dRArea = BoR * HoR;
cout << "The area of the rectangle is " << dRArea;
do{
cout << "What would you like to do? Choose 1-3.\n"
<< "1. Quit?\n"
<< "2. Go back to the menu?\n"
<< "3. Find another rectangle's area?";
cin >> nSecondOS;
switch(nSecondOS)
{
case 1:
return 0;
case 2:
return 2;
case 3:
break;
default:
cout << "You didn't enter a valid number. Try again.";
system("PAUSE");
break;
system("CLS");
}
break;
}while(nSecondOS <= 0 && nSecondOS > 3);
} while(nSecondOS == 3);
return -1;
}
int nOptionThree()
{
int nThirdOS;
do{
system("CLS");
cout << "What is the base of the triangle?\n";
double dBoT = 0.0;
cin >> dBoT;
cout << "What is the hieght of the triangle?\n";
double dHoT = 0.0;
cin >> dHoT;
double dTArea = (dBoT* dHoT) * 0.5;
cout << "The area of the rectangle is " << dTArea;
do{
cout << "What would you like to do? Choose 1-3.\n"
<< "1. Quit?\n"
<< "2. Go back to the menu?\n"
<< "3. Find another rectangle's area?";
cin >> nThirdOS;
switch(nThirdOS)
{
case 1:
return 0;
case 2:
return 2;
case 3:
break;
default:
cout << "You didn't enter a valid number. Try again.";
system("PAUSE");
break;
system("CLS");
}
break;
}while(nThirdOS <= 0 && nThirdOS > 3);
} while(nThirdOS == 3);
return -1;
}
Here - you do the call the same function twice in a row.
1 2 3 4 5 6 7
int nOption = 0;
cin >> nOption;
switch(nOption)
{
case 1:
nOptionOne(); //<- function is called here, but return value isn't stored anywhere
if(nOptionOne() == 0)
//<-function is called again here
This while statement will never be true. A number can't be both less than 0 and greater than 3 at the same time.
Those mistakes were pretty simple. I am sorry I didn't catch them myself. Thank you so much for your help! I believe my program should run properly now.