Nested switch statements and functions

My first time posting here so I dunno if Im doing this right or not....
I am trying to use nested switch statements to find either the area or volume. If i pick something off the first menu (area), when the answer compiles the volume menu still shows up. What am I doing wrong here???
Any help would be greatly appreciated.. THANKS!
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <iomanip>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;

const float PI = 3.14f;

float CircleArea(float radius);
float RectangleArea(float length, float width);
float RightTriangleArea(float base, float height);
float CylinderVolume(float height, float &radius);
float SphereVolume(float &radius);
int menu_choice = 0;
char next_menu_choice = 0;
int number = 0;
float length = 0;
float width = 0;
float base = 0;
float height = 0;
float radius = 0;
float volume = 0;
float area = 0;

int main()
{
	cout << "-- Main Menu --" << endl;
	cout << "1) Calculate Area\n"
		 << "2) Calculate Volume\n";

	cout << "Enter a menu choice: ";
	cin >> menu_choice;
	cout << '\n';

		switch ( menu_choice )
		{
			case 1:
		// Display the area menu
			cout << " -- Area Menu -- \n"
				 << "a) Rectangle\n"
				 << "b) Circle\n"
				 << "c) Right Triangle\n\n";

			cout << "Enter a menu choice: ";
			cin >> next_menu_choice;
			cout << endl;	
			
			
				switch ( next_menu_choice )
				{
				case 'a':
				case 'A':
					cout << "Enter the length: ";
					cin >> length;
					cout << "\nEnter the width: ";
					cin >> width;
					RectangleArea(length, width);
					cout << endl;
					break;

				case 'b':
				case 'B':
					cout << "Enter the radius: ";
					cin >> radius;
					CircleArea(radius);
					cout << endl;
					break;

				case 'c':
				case 'C':
					cout << "Enter the base: ";
					cin >> base;
					cout << "Enter the height: ";
					cin >> height;
					RightTriangleArea(base, height);
					cout << endl;
					break;
				}

			case 2:
			// Display the volume menu
			cout << " -- Volume Menu -- \n"
				 << "a) Cylinder\n"
				 << "b) Sphere\n";
			
				switch (next_menu_choice)
				{
				case 'a':
				case 'A':
					cout << "Enter the height: ";
					cin >> height;
					cout << "Enter the radius: ";
					cin >> radius;
					CylinderVolume(height, radius);
					cout << endl;
					break;

				case 'b':
				case 'B':
					cout << "Enter the radius: ";
					cin >> radius;
					SphereVolume(radius);
					cout << endl;
					break;
				}

			default:
			cout << "You have entered an invalid menu choice.\n"
				 << "Please try again.\n\n";
			break;
			}
		
	system("Pause");
		

		
		}
float CircleArea(float radius)
{
	float area = PI*(radius*radius);
	cout << "The area of a circle with radius " <<radius<< " is: " <<area<< endl;
	return (area);
}
float RectangleArea(float length, float width)
{
	float area = length * width;
	cout << "The area of a rectangle with a length of " <<length<< " and a width of " <<width<< " is: " <<area<< endl;
	return(area);
}
float RightTriangleArea(float base, float height)
{
	float area = (base * height)/2;
	cout << "The area of a right triangle with a base of " 
		 << base
		 << " and a height of " 
		 << height
		 << " is: " 
		 << area << endl;
	return(area); 
}
float CylinderVolume(float height, float &radius)
{
	float volume = (height * PI) * (radius*radius);
	cout << "The volume of a cylinder with a height of " 
		 << height
		 << " and a radius of " 
		 << radius
		 << " is: " 
		 << volume << endl;
	return(volume);
}
float SphereVolume(float &radius)
{
	float volume = (4/3) * PI * (radius*radius*radius);
	cout << "The volume of a sphere with a radius of " 
		 << radius
		 << " is: " 
		 << volume << endl;
	return(volume);
}
Last edited on
No break after case 1:
I tried putting a break after case 1 but then the next switch statement doesnt run. The program just ends.
Where did you put it? It should be just before case 2:
i put it like this...

1
2
3
4
5
6
7
8
9
10
11
12
case 1:
		// Display the area menu
			cout << " -- Area Menu -- \n"
				 << "a) Rectangle\n"
				 << "b) Circle\n"
				 << "c) Right Triangle\n\n";

			cout << "Enter a menu choice: ";
			cin >> next_menu_choice;
			cout << endl;
    break;


but yea....after case 1 it went right to the system pause and ended.hmmm
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
switch ( menu_choice )
		{
			case 1:
		// Display the area menu
			cout << " -- Area Menu -- \n"
				 << "a) Rectangle\n"
				 << "b) Circle\n"
				 << "c) Right Triangle\n\n";

			cout << "Enter a menu choice: ";
			cin >> next_menu_choice;
			cout << endl;	
			
			
				switch ( next_menu_choice )
				{
				case 'a':
				case 'A':
					cout << "Enter the length: ";
					cin >> length;
					cout << "\nEnter the width: ";
					cin >> width;
					RectangleArea(length, width);
					cout << endl;
					break;

				case 'b':
				case 'B':
					cout << "Enter the radius: ";
					cin >> radius;
					CircleArea(radius);
					cout << endl;
					break;

				case 'c':
				case 'C':
					cout << "Enter the base: ";
					cin >> base;
					cout << "Enter the height: ";
					cin >> height;
					RightTriangleArea(base, height);
					cout << endl;
					break;
				}
break;
			case 2:
			// Display the volume menu
			cout << " -- Volume Menu -- \n"
				 << "a) Cylinder\n"
				 << "b) Sphere\n";
			
Ahhhhh I see...Thank you so much!!
Topic archived. No new replies allowed.