Calculator V2.

Ok so this is my revised version of my calculator that you might of seen a few pages back.

I was wondering how I turn all this code into a small nice UI for someone to actually use instead of using it on the terminal or on CMD.

Also can you also leave comments and constructive critisim please on how I can improve it.

The features include:

Addition
Subtraction
Multiplication
Division
Powers
Area
Conversion

I have also only used if, for, switch and functions in this as well.

Thanks

Arc

P.S I am not trying to show off my code at all because I know myself this is a very easy thing to do :)

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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <iostream>
#include <math.h>
using namespace std;

int One;
int Two;
int Three;
int choose;

void conversion() {
	
	cout << "	MENU		\n";
	cout << " 1. Currency \n";
	cout << " 2. Temperature \n";
	
	cin  >> choose;
	
	if (choose == 1) {
	
switch (choose) {
		case 1:
			cout << " 1. British Stirling to Euro \n";
			cout << " 2. British Stirling to US Dollars \n";
			cout << " 3. British Stirling to Canadian Dollars \n";
			cout << " 4. British Stirling to Australian Dollars \n";
			cout << " 5. British Stirling to Japanese Yen \n";
			cout << " 6. British Stirling to Indian Rupees \n";
			cout << " 7. British Stirling to Chinese Renminbi \n";
			cout << " 8. Vise versa \n";
	}
	cin >> choose;
	if (choose == 1) {
		
		cout << "Enter the British Stirling to convert into Euros \n";
		cin >> choose;
		cout << choose * 1.14;
	}
	
	if (choose == 2) {
		
		cout << "Enter the British Stirling to convert into US Dollars \n";
		cin >> choose;
		cout << choose * 1.62;
		
	}
	
	if (choose == 3) {
		
		cout << "Enter the British Stirling to convert into Canadian Dollars \n";
		cin >> choose;
		cout << choose * 1.74;
		
	}
		
	if (choose == 4) {	
		
		cout << "Enter the British Stirling to convert into Australian Dollars \n";
		cin >> choose;
		cout << choose * 1.87;	
		
	}

	if (choose == 5) {
			
		cout << "Enter the British Stirling to convert into Japanese Yen \n";
		cin >> choose;
		cout << choose * 148.67;	
			
	}
	
	if (choose == 6) {
		
		cout << "Indian Rupees \n";
		cin >> choose;
		cout << choose * 78.31;
			
	}
	
	if (choose == 7) {
		
		cout << "Chinese Renminbi \n";
		cin >> choose;	
		cout << choose * 11.11;
			
	}
		
	if (choose == 8) {
			
			cout << " 1. Euro to British Stirling   \n";
			cout << " 2. US Dollars to British Stirling \n";
			cout << " 3. Canadian Dollars to British Stirling \n";
			cout << " 4. Australian Dollars to British Stirling \n";
			cout << " 5. Japanese Yen to British Stirling \n";
			cout << " 6. Indian Rupees to British Stirling \n";
			cout << " 7. Chinese Renminbi to British Stirling \n";
			
		}
	
		switch (choose) {
			case 1:
				cout << "Please enter the Euros to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.90;
				break;
			case 2:
				cout << "Please enter the US Dollars to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.61;
				break;
			case 3:
				cout << "Please enter the Canadian Dollars to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.57;
				break;
			case 4:
				cout << "Please enter the Australian Dollars to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.53;
				break;
			case 5:
				cout << "Please enter the Japanese Yen to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.0067;
				break;
			case 6:
				cout << "Please enter the Chinese Renminbi to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.09;
				break;
			}
				
	}
	
	if (choose == 2) {
		
		cout << " 1. Celcius to Farenheit \n";	
		cout << " 2. Farenheit to Celcius \n";
		
		cin >> choose;	
		
	}
		
	if (choose == 1) {
			
		cout << "Please enter the celcius to convert to farenheit \n";
		cin >> choose;
		cout << choose * 1.8 + 32;
		
	}
	
	if (choose == 2) {
		
		cout << "Please enter the farenheit to convert to celcius \n";
		cin >> choose;
		cout << (choose - 32) / 1.8;	
		
	}
		
}
			
void area() {
	
	cout << "	MENU		\n";
	cout << " 1. Area of a triangle \n";
	cout << " 2. Area of a square \n";
	cout << " 3. Area of a rectangle \n";
	cout << " 4. Area of a circle \n";
	cout << " 5. Area of a trapezium \n";
	
	cin >> choose;
	
switch (choose) {
	case 1:
		cout << "Please enter the base length of the triangle, followed by the height. ";
		cin >> One >> Two;
		cout << "The area of the triangle is " << One / 2 * Two << " " ;
		break;
	case 2:
		cout << "Please enter the length of one of the sides of the square please. ";
		cin >> One;
		cout << "The area of the square is " << One * One << " " ;
		break;
	case 3:	
		cout << "Please enter the length of the rectangle, followed by the height. ";
		cin >> One >> Two;
		if (One == Two) {
		cout << "Thats not the dimensions of a rectangle! They have to be different! ";
			area();
		}
		else {
			cout << "The area of the rectangle is " << One * Two << " " ;
			break;
		}

	case 4:
		Two = 3.14;
		cout << "Please enter the radius of the cirlce please. ";
		cin >> One;
		cout << "The area of the cirlce is " << Two * One * One << " " ;
		break;
	case 5:
		cout << "Please enter the length of the 2 parrallel sides and also the height. ";
		cin >> One >> Two >> Three;
		cout << "The area of the trapezium is " << (One + Two) * 0.5 * Three << " ";
		break;
	}
}

void powers() {
float power;
	
	cout << "	MENU		\n";
	cout << " 1. Powers \n";
	cout << " 2. Square roots \n";
	cin >> choose;
	
	if (choose == 1) {
		
		cout << "Please enter the number you would like to square and the number of times you would like to times it by it. ";
			cin >> One >> Two;
			cout << "\t" << pow(One, Two) << " " ;
	}
			
	if (choose == 2) {
		
			cout << "Enter a number to find its square route. ";
			cin >> power;
			cout << "\t" << sqrt(power) << " " ;
		
	}

}
	
void division() {
	
	cin >> One >> Two;
	cout << "\t" << One / Two << " " ;
	
}
	
void multiplication() {
	
	cin >> One >> Two;
	cout << "\t" << One * Two << " " ;
	
}

void addition() {

	cin >> One >> Two;
	cout << "\t" << One + Two << " " ;
	
}

void subtraction() {
	
	cin >> One >> Two;
	cout << "\t" << One - Two << " " ;
	
}

int main () {

	system ("clear");
	
	cout << "	MENU		\n";
	cout << " 1. Subtraction\n";
	cout << " 2. Addition\n";
	cout << " 3. Multiplication\n";
	cout << " 4. Division\n";
	cout << " 5. Powers\n";
	cout << " 6. Areas\n";
	cout << " 7. Conversions\n";
	cout << " 8. Quit\n";
	
	cin >> choose;
	
	switch (choose) {
		case 1:
			subtraction();
			break;
		case 2:
			addition();
			break;
		case 3:
			multiplication();
			break;
		case 4:
			division();
			break;
		case 5:
			powers();
			break;
		case 6:
			area();
			break;
		case 7:
			conversion();
			break;
		case 8:
			break;
	
	}
	
		return 0;
}


ARGH it burns! So much code!!
Last edited on
closed account (S6k9GNh0)
Now make it in a GUI using wxWidgets or GCJ.
Ok thanks. Just what I was looking for.
The conversion function isn't quite right.
why what is wrong?
Your case statements and if statements are not written correctly:
Your conversion function is like this:
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
void conversion() {
	
	cout << "	MENU		\n";
	cout << " 1. Currency \n";
	cout << " 2. Temperature \n";
	
	cin  >> choose;
	
	if (choose == 1) {
	
switch (choose) {
		case 1:
			cout << " 1. British Stirling to Euro \n";
			cout << " 2. British Stirling to US Dollars \n";
			cout << " 3. British Stirling to Canadian Dollars \n";
			cout << " 4. British Stirling to Australian Dollars \n";
			cout << " 5. British Stirling to Japanese Yen \n";
			cout << " 6. British Stirling to Indian Rupees \n";
			cout << " 7. British Stirling to Chinese Renminbi \n";
			cout << " 8. Vise versa \n";
	}
	cin >> choose;
	if (choose == 1) {
		
		cout << "Enter the British Stirling to convert into Euros \n";
		cin >> choose;
		cout << choose * 1.14;
	}
	
	if (choose == 2) {
		
		cout << "Enter the British Stirling to convert into US Dollars \n";
		cin >> choose;
		cout << choose * 1.62;
		
	}
	
	if (choose == 3) {
		
		cout << "Enter the British Stirling to convert into Canadian Dollars \n";
		cin >> choose;
		cout << choose * 1.74;
		
	}
		
	if (choose == 4) {	
		
		cout << "Enter the British Stirling to convert into Australian Dollars \n";
		cin >> choose;
		cout << choose * 1.87;	
		
	}

	if (choose == 5) {
			
		cout << "Enter the British Stirling to convert into Japanese Yen \n";
		cin >> choose;
		cout << choose * 148.67;	
			
	}
	
	if (choose == 6) {
		
		cout << "Indian Rupees \n";
		cin >> choose;
		cout << choose * 78.31;
			
	}
	
	if (choose == 7) {
		
		cout << "Chinese Renminbi \n";
		cin >> choose;	
		cout << choose * 11.11;
			
	}
		
	if (choose == 8) {
			
			cout << " 1. Euro to British Stirling   \n";
			cout << " 2. US Dollars to British Stirling \n";
			cout << " 3. Canadian Dollars to British Stirling \n";
			cout << " 4. Australian Dollars to British Stirling \n";
			cout << " 5. Japanese Yen to British Stirling \n";
			cout << " 6. Indian Rupees to British Stirling \n";
			cout << " 7. Chinese Renminbi to British Stirling \n";
			
		}
	
		switch (choose) {
			case 1:
				cout << "Please enter the Euros to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.90;
				break;
			case 2:
				cout << "Please enter the US Dollars to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.61;
				break;
			case 3:
				cout << "Please enter the Canadian Dollars to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.57;
				break;
			case 4:
				cout << "Please enter the Australian Dollars to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.53;
				break;
			case 5:
				cout << "Please enter the Japanese Yen to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.0067;
				break;
			case 6:
				cout << "Please enter the Chinese Renminbi to convert to British Stirling \n";
				cin >> choose;
				cout << choose * 0.09;
				break;
			}
				
	}
	
	if (choose == 2) {
		
		cout << " 1. Celcius to Farenheit \n";	
		cout << " 2. Farenheit to Celcius \n";
		
		cin >> choose;	
		
	}
		
	if (choose == 1) {
			
		cout << "Please enter the celcius to convert to farenheit \n";
		cin >> choose;
		cout << choose * 1.8 + 32;
		
	}
	
	if (choose == 2) {
		
		cout << "Please enter the farenheit to convert to celcius \n";
		cin >> choose;
		cout << (choose - 32) / 1.8;	
		
	}
		
}


Lets say the user selects British Stirling to Euro \n"; then choose will be 1 (line 25)
The user enter 2 British sterling to be converted - Now choose == 2 (line 28).
Now choose is checked on line 32 and the user will be asked "Enter the British Stirling to convert into US Dollars \n"
Lets say that the user enters 2 again.
Now choose is checked at line 92 - choose is 2 - so the user will be asked for << "Please enter the US Dollars to convert to British Stirling \n" (Line 99)
Lets say the user asks for 1 US Dollar to be converted - choose ==1 (line 100).
Now choose is checked again (Line 136) and will ask cout << "Please enter the celcius to convert to farenheit \n";




As I'm sure you're aware, you use the variable "choose" for just about everything to drive your menu selection. Perhaps as an afterthought you may want to place some inline comments to help any other programmer reading your code as to what you expect the value of choose to do after you have read it in. This is what I mean:

if (choose == 8) {

cout << " 1. Euro to British Stirling \n";
cout << " 2. US Dollars to British Stirling \n";
cout << " 3. Canadian Dollars to British Stirling \n";
cout << " 4. Australian Dollars to British Stirling \n";
cout << " 5. Japanese Yen to British Stirling \n";
cout << " 6. Indian Rupees to British Stirling \n";
cout << " 7. Chinese Renminbi to British Stirling \n";

}

switch (choose) {
case 1:
cout << "Please enter the Euros to convert to British Stirling \n";
cin >> choose;
cout << choose * 0.90;
break;
case 2:
cout << "Please enter the US Dollars to convert to British Stirling \n";
cin >> choose;
cout << choose * 0.61;
break;
case 3:
cout << "Please enter the Canadian Dollars to convert to British Stirling \n";
cin >> choose;
cout << choose * 0.57;
break;
case 4:
cout << "Please enter the Australian Dollars to convert to British Stirling \n";
cin >> choose;
cout << choose * 0.53;
break;
case 5:
cout << "Please enter the Japanese Yen to convert to British Stirling \n";
cin >> choose;
cout << choose * 0.0067;
break;
case 6:
cout << "Please enter the Chinese Renminbi to convert to British Stirling \n";
cin >> choose;
cout << choose * 0.09;
break;
}

}

if (choose == 2) {

cout << " 1. Celcius to Farenheit \n";
cout << " 2. Farenheit to Celcius \n";

cin >> choose;

}

if (choose == 1) {

cout << "Please enter the celcius to convert to farenheit \n";
cin >> choose;
cout << choose * 1.8 + 32;

}

if (choose == 2) {

cout << "Please enter the farenheit to convert to celcius \n";
cin >> choose;
cout << (choose - 32) / 1.8;

}

Okay, from your menu, I am supposed to be able to select 7. There is no outcome for choose ==7 in your switch. Also for these last few if blocks, I'd have to keep following you through your switch to even figure out what is going on at the end. Just some //inline comments would be snazzy here.


Also I note that you do not have a default switch statement. The user could easily break your code by typing a char or string instead of an integer. Possibly using a try/catch on your cin statements could be another option.

Also I noticed in your conversion() function that you start out by writing a switch statement with only one outcome, and then proceed to use if statements if(choose ==1)...if(choose ==2) for a large number of other outcomes. This doesnt make much sense to me, I would think you'd put this the other way around. If you just want it to print that menu, perhaps a do{ }while(choose != 0) or whatever you want the exit integer to be. There should be some way for the user to enter a function and then decide they dont want to proceed and return to previous menu at EVERY step in your program.

Great work though, hope i'm not too critical!
Your code is still very poorly constructed. You should really take all the input in your main function and pass it through to your functions, which should then return the result to be output in your main function. There's also absolutely no need for any global variables.

And in C++ you should be including <cmath> rather than <math.h>.
Last edited on
Topic archived. No new replies allowed.