Volume part of calculator not working

Right now I am programming a calculator, and I am on to geometry. I have the user able to choose if they want area or volume. If the user chooses area, everything works fine. If the user chooses volume, the program will act as if it ended and follows through with my "Do you want to continue[Y/N]" loop.

Note: geometry code starts at line 101.


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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

#include <iostream>
#include <cmath>

using namespace std;
int main(int argc, char *argv[]) {
	char c;
	do{
		
	
	string type;
	string oper;
	string shape;
	string gtype;
	//Basic math terms
	float a;
	float b;
	float answ;
	//Geometry
	float base;
	float height;
	float side;
	float area;
	float base1;
	float base2;
	float length;
	float width;
	
		
	
	//To compile with Terminal, use the "g++ calculator.cpp -o calculator" command
	
		
		cout << endl << "Basic math = bm, Geometry = g, Square root = s. Power = p. Type end for the program to end.";
		cin >> type;
		
		//End program code
		if(type=="end")
		{
			cout << "Bye!";
			return 0;
		}
		
		
		//Specifys +,-,*, and /
		if(type=="bm")
		{
		cout << endl << "Addition, Subtraction, Multiplication, Division. Your choice? ";
		
		cin >> oper;
		if(oper=="addition")
		{
			cout << "Enter a number: ";
			cin >> a;
			cout << "A number to add to " << a << ": ";
			cin >> b;
			answ = a + b;
			cout << endl << "Your sum is " << answ << endl;
		}
		
		else if(oper=="subtraction")
		{
			cout << "Enter a number: ";
			cin >> a;
			cout << "A number to subtract from " << a << ": ";
			cin >> b;
			answ = a - b;
			cout << endl << "Your difference is " << answ << endl;
		}
		
		else if(oper=="multiplication")
		{
			cout << "Enter a number: ";
			cin >> a;
			cout << "A number to multiply " << a << " by: ";
			cin >> b;
			answ = a * b;
			cout << endl << "Your product is " << answ << endl;
		}
		
		else if(oper=="division")
		{
			cout << "Enter a number:";
			cin >> a;
			cout << "Enter a number to divide " << a << " by: ";
			cin >> b;
			answ = a / b;
			cout<< endl << "Your quotient is " << answ << endl;
			
		}
		else
			{
		    cout << "Invalid input" << endl;
			}

		//Ends basic math code
		}
		
		
		//Starts geometry
		if(type=="g")
		{
		cout << "Do you want to find the area or volume of a shape? ";
		cin >> gtype;
		
		
					
		
		if(gtype=="area"){	
		cout << "Do you want to find the area of a square, a rectangle, a trapezoid, a parallelogram, or a triangle?";
		cin >> shape;
		
		
		if(shape=="square"){
			cout << "Enter the length of one side: ";
			cin >> side;
			answ = side * side;
			cout << "The area of the square is: " << answ;
			
		}
		
		else if(shape=="rectangle"){
			cout << "Enter the base: ";
			cin >> base;
			cout << "Enter the height: ";
			cin >> height;
			answ = base * height;
			cout << "The area of the rectangle is " << answ;
		}
		
		else if(shape=="trapezoid"){
			cout << "Enter base one: ";
			cin >> base1;
			cout << "Enter base two: ";
			cin >> base2;
			cout << "Enter the height: ";
			cin >> height;
			
			answ = .5 * (base1+base2) * height;
			
			cout << "The area of the trapezoid is " << answ;
			
		}
		
		else if(shape=="parallelogram"){
			cout << "Enter the base: ";
			cin >> base;
			cout << "Enter the height: ";
			cin >> height;
			answ = base * height;
			cout << "The area of your parallelogram is " << answ;
		}
		
		else if(shape=="triangle"){
			cout << "Enter the base: ";
			cin >> base;
			cout << "Enter the height: ";
			cin >> height;
			answ = .5*(base*height);
			cout << "The area of your triangle is " << answ;
		}
		
		else
		{
		     cout << "Invalid input" << endl;
		}
		
		if(gtype=="volume"){
			cout << "Do you want to find the volume of a rectangular prism, or a cube? ";
			cin >> shape;
	
		if(shape=="rectangular prism"){
			cout << "Enter the length: ";
			cin >> length;
			cout << "Enter the width: ";
			cin >> width;
			cout << "Enter the height: ";
			answ = length*width*height;
			cout << "The volume of the rectangular prism is " << answ << " .";
		}
			
		//Ends geometry code
		}
		}
		}
		
		
		//Starts square root code
		if(type=="s"){
			cout << "What number do you want to see the square root of? ";
			cin  >> a;
			answ = sqrt(a);
			cout << "The square root of " << a << " is " << answ << " .";
			
		}
		
		if(type=="p"){
			cout << "Enter the base: ";
			cin >> a;
			cout <<"Enter the power: ";
			cin >> b;
			cout << a << " to the power of " << b << " is " << pow(a,b) << " .";
		}
		
		
		
	cout<< endl <<"Do you want to continue (Y/N)?" << endl;
	cin>>c ;
	}while(c=='y'||c=='Y');
	if(c == 'n'){
		cout << "Program ended" << endl;
	}
}
You don't close your if statement for the first gtype evaluation on line 109 before the statement on 168.

So the if statement on line 168 to see if the user inputted "volume" is inside the if statement for area. So it will never evaluate to true.
you have your braces messed up. your code should look like this comments where braces are changed

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include<string>
#include <cmath>

using namespace std;
int main(int argc, char *argv[]) {
	char c;
	do{
		
	
	string type;
	string oper;
	string shape;
	string gtype;
	//Basic math terms
	float a;
	float b;
	float answ;
	//Geometry
	float base;
	float height;
	float side;
	float area;
	float base1;
	float base2;
	float length;
	float width;
	
		
	
	//To compile with Terminal, use the "g++ calculator.cpp -o calculator" command
	
		
		cout << endl << "Basic math = bm, Geometry = g, Square root = s. Power = p. Type end for the program to end.";
		cin >> type;
		
		//End program code
		if(type=="end")
		{
			cout << "Bye!";
			return 0;
		}
		
		
		//Specifys +,-,*, and /
		if(type=="bm")
		{
		cout << endl << "Addition, Subtraction, Multiplication, Division. Your choice? ";
		
		cin >> oper;
		if(oper=="addition")
		{
			cout << "Enter a number: ";
			cin >> a;
			cout << "A number to add to " << a << ": ";
			cin >> b;
			answ = a + b;
			cout << endl << "Your sum is " << answ << endl;
		}
		
		else if(oper=="subtraction")
		{
			cout << "Enter a number: ";
			cin >> a;
			cout << "A number to subtract from " << a << ": ";
			cin >> b;
			answ = a - b;
			cout << endl << "Your difference is " << answ << endl;
		}
		
		else if(oper=="multiplication")
		{
			cout << "Enter a number: ";
			cin >> a;
			cout << "A number to multiply " << a << " by: ";
			cin >> b;
			answ = a * b;
			cout << endl << "Your product is " << answ << endl;
		}
		
		else if(oper=="division")
		{
			cout << "Enter a number:";
			cin >> a;
			cout << "Enter a number to divide " << a << " by: ";
			cin >> b;
			answ = a / b;
			cout<< endl << "Your quotient is " << answ << endl;
			
		}
		else
			{
		    cout << "Invalid input" << endl;
			}

		//Ends basic math code
		}
		
		
		//Starts geometry
		if(type=="g")
		{
		cout << "Do you want to find the area or volume of a shape? ";
		cin >> gtype;
		
		
					
		
		if(gtype=="area"){	
		cout << "Do you want to find the area of a square, a rectangle, a trapezoid, a parallelogram, or a triangle?";
		cin >> shape;
		
		
		if(shape=="square"){
			cout << "Enter the length of one side: ";
			cin >> side;
			answ = side * side;
			cout << "The area of the square is: " << answ;
			
		}
		
		else if(shape=="rectangle"){
			cout << "Enter the base: ";
			cin >> base;
			cout << "Enter the height: ";
			cin >> height;
			answ = base * height;
			cout << "The area of the rectangle is " << answ;
		}
		
		else if(shape=="trapezoid"){
			cout << "Enter base one: ";
			cin >> base1;
			cout << "Enter base two: ";
			cin >> base2;
			cout << "Enter the height: ";
			cin >> height;
			
			answ = .5 * (base1+base2) * height;
			
			cout << "The area of the trapezoid is " << answ;
			
		}
		
		else if(shape=="parallelogram"){
			cout << "Enter the base: ";
			cin >> base;
			cout << "Enter the height: ";
			cin >> height;
			answ = base * height;
			cout << "The area of your parallelogram is " << answ;
		}
		
		else if(shape=="triangle"){
			cout << "Enter the base: ";
			cin >> base;
			cout << "Enter the height: ";
			cin >> height;
			answ = .5*(base*height);
			cout << "The area of your triangle is " << answ;
		}
		}//CLOSES AREA <<<<<<<<< THIS FIXES PROBLEM CLOSES AREA IF 
		else
		{
		     cout << "Invalid input" << endl;
		}
		
		if(gtype=="volume"){
			cout << "Do you want to find the volume of a rectangular prism, or a cube? ";
			cin >> shape;
	
		if(shape=="rectangular prism"){
			cout << "Enter the length: ";
			cin >> length;
			cout << "Enter the width: ";
			cin >> width;
			cout << "Enter the height: ";
			answ = length*width*height;
			cout << "The volume of the rectangular prism is " << answ << " .";
		}
			
		//Ends geometry code
		}//CLOSES VOLUME
		}//CLOSES GEOMETRY 
		//} <<<<<<THIS WAS THE PROBLEM THIS BRACE 
		//Starts square root code
		if(type=="s"){
			cout << "What number do you want to see the square root of? ";
			cin  >> a;
			answ = sqrt(a);
			cout << "The square root of " << a << " is " << answ << " .";
			
		}
		
		if(type=="p"){
			cout << "Enter the base: ";
			cin >> a;
			cout <<"Enter the power: ";
			cin >> b;
			cout << a << " to the power of " << b << " is " << pow(a,b) << " .";
		}
		
		
		
	cout<< endl <<"Do you want to continue (Y/N)?" << endl;
	cin>>c ;
	}while(c=='y'||c=='Y');
	if(c == 'n'){
		cout << "Program ended" << endl;
	}

}
Thanks, that fixed the volume problem.

However, when I choose rectangular prism, the program will say "Invalid Input" but will come up with the

"Do you want to find the volume of a rectangular prism, or a cube? "

After choosing rectangular prism, the program says "Do you want to continue (Y/N)?" and then ends immediately.

@tag

That didn't seem to make a difference. The rectangular prism part is still broken.
Last edited on
Figured it out! The rectangular prism was broken because a string can only contain one word(no spaces)

Thanks
Topic archived. No new replies allowed.