Problem with switch...I think?

Hello, I am having trouble with a program that is meant to calculate the volume of a sphere, cylinder, and so on. I suspect I have a small error in my code causing it to skip over the volume statements for the sphere. If anyone could give it a quick look, I would greatly appreciate some help, 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
#include <iostream>
#include <math.h>
#define PI 3.1415926535897932384626

using namespace std;

int main()
{
start:
char restart;
char shape;
double radius;
double height;
int sides;
double halfsidelength;
double sidelength;
double apothem;
double Base;
double perimeter;
double slantheight;
char sora;
char slantheightq1;

cout << "Geometric figure surface area and volume calculator\n";
cout << "What kind of shape are you using?\n";
cout << "Sphere(s), Cylinder(y), Prism(p), Pyramid(d)";
cin >> shape;

switch(shape){





	case 's':
		cout << "Are you calculating the surface area(s) or the volume(v)?";
		cin >> sora;
		if(sora=='s'){//This is for the surface area
			cout << "Enter the radius: ";
			cin >> radius;
			cout << "Surface Area = "<<(4*PI*(radius*radius))<<endl;
			cout << "Would you like to restart the program?(Y/N)";}
			cin >> restart;
			if(restart=='y'){
				goto start;}
			else if(restart=='n'){
				cout << "Thank you for using the geometric figure calculator";}
		else if(sora=='v'){//This is for the volume
			cout << "Enter the radius: ";
			cin >> radius;
			cout << "Volume = "<<((4*PI*(radius*radius*radius))/3);
			cout << "Would you like to restart the program?(Y/N)";}
			cin >> restart;
			if(restart=='y'){
				goto start;}
			else if(restart=='n'){
				cout << "Thank you for using the geometric figure calculator";}
			
	break;



	case 'y':
		cout << "Are you calculating the surface area(s) or the volume(v)?";
		cin >> sora;
		if(sora=='s'){
			cout << "Enter the radius: ";
			cin >> radius;
			cout << "Enter the height: ";
			cin >> height;
			cout << "Surface Area = "<<((2*(PI*(radius*radius))+(2*(PI*radius)*height)));
			cout << "\nWould you like to restart the program?(Y/N)";}
						cin >> restart;
						if(restart=='y'){
							goto start;}
						else if(restart=='n'){
							cout << "Thank you for using the geometric figure calculator";}
		else if(sora=='v'){
			cout << "Enter the radius: ";
			cin >> radius;
			cout << "Enter the height: ";
			cin >> height;
			cout << "Volume = "<<(PI*(radius*radius)*height);
			cout << "Would you like to restart the program?(Y/N)";}
						cin >> restart;
						if(restart=='y'){
							goto start;}
						else if(restart=='n'){
							cout << "Thank you for using the geometric figure calculator";}
	break;

default:
		cout << "I apologize, we do not currently support this figure\n";
		cout << "Would you like to restart the program?(Y/N)";
		cin >> restart;
		if(restart=='y'){
			goto start;}
		else if(restart=='n'){
			cout << "Thank you for using the geometric figure calculator\n";}
	break;
}
return(0);
}  
The logic of your program seems to be sound, but see this example. It's a lot cleaner code to look at, therefore easier to debug and the interface for the user much simpler.

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
#include <iostream>
#include <math.h>

	const double PI = 3.1415926535897932384626;

using namespace std;

int main() {
	cout << "Geometric figure surface area and volume calculator\n\n";
	cout << "\t[S] Sphere\n\t[C] Cylinder\n\t[P] Prisim\n\t[Y] Pyramid\n\t[Q] Quit\n";
	
	do	{
		char Selection, SubSelection;
		double	Result, Radius;
		
		cout << "\nPlease choose S C P Y or Q: ";
		cin	>> Selection;
		Selection &= 0x5F;			// Convert to uppercase
		
		if ( Selection == 'Q' )
			break;					// Terminate infinite loop		
				
		switch ( Selection ) {
			case 'S':
				cout << "\nSpheres radius? ";
				cin >> Radius;
				
				Result = 4 * PI * (Radius * Radius);
				cout << "\nSurface Area = " << Result << endl;
				
				Result = (4 * PI * (Radius * Radius * Radius)) / 3;
				cout << "      Volume = " << Result << endl;
				break;
				
			case 'Y':
				break;
				
			case 'P':				
			case 'C':
				cout << "\tSorry " << Selection << " has not been implemented yet\n";
				break;
				
			default:
				cout << "\t\"" << Selection << "\" is not a valid selection\n";
			}
						
		} while (1);
		
	cout << "\nBye Bye\n";
	return(0);
	}


Try to reuse variables as much as possible as most declarations in lines 10 - 22 in your example can be accommodated by Result. You shouldn't have too much problem implementing for the other selections.
Thank you for the help, I will try to be a little more organized. I will reuse variables, which hadn't occurred to me.lol
Topic archived. No new replies allowed.