Basically the whole purpose of this program is to prompt the user to use a calculator. Choices 1-6 are valid, but I want to set it up where selecting any other number outside of 1-6 to be Invalid, and will display an 'Invalid Choice' message, and then go back to the main menu. Been trying to get this for the past 2 hours, and it's a headache (I'm a noob to programming). The main program does work properly, it's the 'Invalid' setup that is giving me problems
Any suggestions or help is greatly appreciated!
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int loop=1;
int choice;
while (loop!=6)
{
system("CLS");
cout << "::Program 3: Geometry Calculator (The Expanded Edition)::\n\n" //Main Menu
<< "1. Area of a Circle\n"
<< "2. Area of a Triangle\n"
<< "3. Area of a Parallelogram\n"
<< "4. Area of a Trapezoid\n"
<< "5. Area of an Ellipse\n"
<< "6. Exit\n\n";
cin >> choice;
switch(choice)
{
case 1: //Area of circle coding.
system("CLS");
float pi, radius, area;
cout << "::Area of a Circle::\n\n"
<< "Enter your radius: ";
cin >> radius;
pi = 3.14159;
area = pi * (radius*radius);
cout << "The area of the circle is " << area << "." << endl;
system("PAUSE");
}
switch(choice)
{
case 2: //Area of triangle coding.
if(choice==2)
system("CLS");
float base, height, a2;
cout << "::Area of a Triangle::\n\n"
<< "Enter your base: ";
cin >> base;
cout << "Enter your height: ";
cin >> height;
a2 = base * height / 2;
cout << "Your result is " << a2 << "." << endl;
system("PAUSE");
}
switch(choice)
{
case 3: //Area of parallelogram coding.
if(choice==3)
system("CLS");
float b2, h2, a3;
cout << "::Area of a Parallelogram::\n\n"
<< "Enter your base: ";
cin >> b2;
cout << "Enter your height: ";
cin >> h2;
a3 = b2 * h2;
cout << "Your result is " << a3 << "." << endl;
system("PAUSE");
}
switch(choice)
{
case 4: //Area of trapezoid coding.
if(choice==4)
system("CLS");
float b3, b4, h3, a4;
cout << "::Area of a Trapezoid::\n\n"
<< "Enter your first base: ";
cin >> b3;
cout << "Enter your second base: ";
cin >> b4;
cout << "Enter your height: ";
cin >> h3;
a4 = (b3 + b4) / 2 * h3;
cout << "Your result is " << a4 << "." << endl;
system("PAUSE");
}
switch(choice)
{
case 5: //Area of Ellipse coding.
if(choice==5)
system("CLS");
float smaa, smia, a5, pi2;
cout << "::Area of an Ellipse::\n\n"
<< "Enter your Semi-Major Axis: ";
cin >> smaa;
cout << "Enter your Semi-Minor Axis: ";
cin >> smia;
pi2 = 3.14159;
a5 = pi2 * smaa * smia;
cout << "Your result is " << a5 << "." << endl;
system("PAUSE");
}
switch(choice)
{
case 6: //Exit coding.
if(choice==6)
system("CLS");
int choice = 0;
std::cout << 'P';
do
{
std::cout << "lease enter a whole number in the range [1, 5]: " << std::flush;
std::cin >> choice;
if(!std::cin || choice < 1 || choice > 5)
{
std::cin.clear();
choice = 0;
std::cout << "Invalid response, p";
}
else
{
break;
}
}while(true);
std::cout << "The number you entered, " << choice << ", was acceptable." << std::endl;
Please enter a whole number in the range [1, 5]: 7
Invalid response, please enter a whole number in the range [1, 5]: 2
The number you entered, 2, was acceptable.