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

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <iomanip>

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:
Enter your radius: ....
Your Area is:.....

Press any key to continue....

*Main Menu Screen*
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.