You are using a version without Ads of this website. Please, consider donating:

### Setting up an Invalid Response

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"

pi = 3.14159;

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");

cout << "Good-bye!\n\n";
system("PAUSE");
exit(0);
}
switch(choice)
{
if(choice!=6)
system("CLS");

cout << "Your choice is invalid";
system("PAUSE");
}

}
}

Last edited on
 ``123456789101112131415161718`` ``````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.```
Last edited on
No need to use multiple switch statements

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136`` ``````#include #include using namespace std; void message() { 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"; } int main() { int choice = 0; message(); while (1) { cout << "\nEnter one of the options please\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"); break; case 2: //Area of triangle coding. 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"); break; case 3: //Area of parallelogram coding. 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"); break; case 4: //Area of trapezoid coding. 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"); break; case 5: //Area of Ellipse coding. 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"); break; case 6: //Exit coding. system("CLS"); cout << "Good-bye!\n\n"; system("PAUSE"); return 0; break; default: cerr << "Your choice is invalid\n\n"; message(); system("PAUSE"); break; } } }``````
So you can combine all switch statements into one long code. I was always under the impression that each one had to have it's own { } set
You only need to use {} if you want to declare variables in a case.
Ahhh ok. Now if I wanted to have it loop back to the main menu after completing each case, would I need to put a loop statement to do this?
It looks like Smac's code already does this.
It asks for a choice at the end of each choice. What I want to modify is have it loop back to the main menu screen at the end.

Like it would be:

Press any key to continue....

Move line 21 to line 24 (referencing line numbers in Smac's post)
That did it. Thank you Smac and L B for your help. Just a simple operation takes a lot of coding.

Starting to like programming hehe
ET21 wrote:
Just a simple operation takes a lot of coding.
This isn't "a lot of coding", it's just tedious coding because you haven't learned how to factor & refactor code yet ;)
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: