Help with Calculator

I'm trying to create a claculator that asks: For each computation, before getting the numbers, ask the user if they want to perform:
• integer arithmetic (whole numbers input and whole number results); or
• floating point arithmetic (input numbers may contain a decimal, and output must contain a decimal).
and has the following menus:

CALCULATOR PRACTICE PROGRAM
Hello, and welcome to the Calculator Practice Program.
This program allows you to practice your math skills.
Choose what you want to practice in the menu shown below.
-----------------------------------------------
MAIN MENU
-----------------------------------------------
a. Addition (X+Y)
b. Subtraction (X-Y)
c. Multiplication (X*Y)
d. Division (X/Y)
e. Powers & Roots
q. Quit the program
-----------------------------------------------
Enter your choice [ a - e, q ]:
-----------------------------------------------

Powers & Roots Menu
Hello, and welcome to the Powers & Roots Menu.
This menu allows you to take powers and roots of a number.
---------------------------------------------------
POWERS & ROOTS MENU
---------------------------------------------------
a. Square a number (Xˆ2)
b. Cube a number (Xˆ3)
c. Raise to any power (XˆY)
d. Square root a number (Xˆ1/2)
e. Cube root a number (Xˆ1/3)
f. Take any root (Xˆ1/Y)
m. Return to Main Menu
q. Quit the program
---------------------------------------------------
Enter your choice [ a - f, m, q ]:
---------------------------------------------------

This is what i have so far ;
#include <cstdlib>
#include <iostream>

using namespace std;

int displayMenu()
{
int choice;

cout << " \n Hello, and welcome to the Calculator Practice Program.
This program allows you to practice your math skills.
Choose what you want to practice in the menu shown below.\n\n"

<< " (a) Addition\n"
<< " (b) Subtraction\n"
<< " (c) Division\n"
<< " (d) Mulpilication\n"
<< " (e) Powers & Roots\n"
<< " (q) Exit\n\n"
<< " Enter Your Choice[a-e,q ]: ";
cin >> choice;
return choice;
}

int main(int argc, char *argv[])
{
int menu;
float total, num1, num2;

menu = displayMenu();
system("CLS");

if (menu == q)
{
cout << " Thank you for using the Calculator\n\n ";
cin.get();

}
else
{
switch (menu)
{
case a:
int values;
cout << " How many numbers would you like to enter? : ";
cin >> values;
float numbers[values];

for(int counter=0; counter<values; counter++)
{
cout << counter+1 << ") Enter number: ";
cin >> numbers[counter];
}
for(int counter=0; counter<values; counter++)
total+=numbers[counter];
cout << "\n The Answer is : " << total << "\n\n ";
system("PAUSE");
break;
case b: // Subtraction Completed
cout << " Enter first number: ";
cin >> num1;
cout << " Enter second number: ";
cin >> num2;
total=(num1-num2);
cout << "\n The Answer is : " << total << "\n\n ";
break;
case c: // Divison Completed
cout << " Enter first number: ";
cin >> num1;
cout << " Enter second number: ";
cin >> num2;
total=(num1/num2);
cout << "\n The Answer is : " << total << "\n\n ";
break;
case d: // Multiplication
cout << " Enter first number: ";
cin >> num1;
cout << " Enter second number: ";
cin >> num2;
total=(num1*num2);
cout << "\n The Answer is : " << total << "\n\n ";

}
system("PAUSE");
return EXIT_SUCCESS;
}

/*case 4: // Multiplication
for(int counter=1; counter<values; counter++)
total=(numbers[0]*numbers[counter]);
cout << "\n The Answer is : " << total << "\n\n "; */

system("PAUSE");
return EXIT_SUCCESS;
}
pow(x,y) is the power function x^y

the root of something is 1/root power. That is, the square root is pow(x, 0.5) and the cubed root is pow(x, 1/3.0)
and so on.

I think with that, you should be able to do it now?


pow is in the header <cmath>

to do floating point, you want double types.
rather than have a billion if statements or a massive case statement, you can do this:

double ad, bd, totald;
int ai, bi, totali;
...
case d:
cin etc
totali = ai*bi;
totald = ad*bd; //compute each answer both ways.

...
and at the end once and for all decide which result to show.

if(wanted_doubles)
cout << words << totald;
else
cout << words << totali;


it might also be best to read the numbers ONCE at the top, and THEN go into the case statement. You *know* you need to read the 2 values each time. Factor that out of the code to reduce the bulk

It is probably some sort of heresy to not have a default case in every switch statement :)

default:
cout << "invalid choice and stuff\n";

it is also highly likely you need to case 'd' not case d -- d being a variable you don't have, and 'd' being the character d which was entered by the user.

Last edited on
closed account (48T7M4Gy)
This repairs a few things:
1. The menu strings
2. Menu returns a char istead of int. Especially useful for ignoring case of choice.
3. Note braces required in case a.
4. 'q' is just another choice so if ... else structure isn't needed by the look of it.
5. It's good practice to have a default case. Look that up in the switch control tutorial on this site. (I just noticed jonnin says something about default cases. I'd just put one in and not get bogged down in religion.)
6. I haven't checked the arithmetic.
7. And, I nearly forgot, use doubles instead of floats and initialize variables (ie set a zero or other deliberate value at the declaration stage.

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
#include <cstdlib>
#include <iostream>

using namespace std;

char displayMenu_01()
{
    char choice;
    
    cout
    << " \n Hello, and welcome to the Calculator Practice Program."
    << "    This program allows you to practice your math skills."
    << "Choose what you want to practice in the menu shown below.\n\n"
    
    << " (a) Addition\n"
    << " (b) Subtraction\n"
    << " (c) Division\n"
    << " (d) Mulpilication\n"
    << " (e) Powers & Roots\n"
    << " (q) Exit\n\n"
    << " Enter Your Choice[a-e,q ]: ";
    cin >> choice;
    return choice;
}

char displayMenu_02()
{
    char choice;
    
    cout
    << "---------------------------------------------------\n"
    << "POWERS & ROOTS MENU\n"
    << "---------------------------------------------------\n"
    << "a. Square a number (Xˆ2)\n"
    << "b. Cube a number (Xˆ3)\n"
    << "c. Raise to any power (XˆY)\n"
    << "d. Square root a number (Xˆ1/2)\n"
    << "e. Cube root a number (Xˆ1/3)\n"
    << "f. Take any root (Xˆ1/Y)\n"
    << "m. Return to Main Menu\n"
    << "q. Quit the program\n"
    << "---------------------------------------------------\n"
    << "Enter your choice [ a - f, m, q ]:\n"
    << "---------------------------------------------------\n";
    
    cin >> choice;
    return choice;
}


int main()
{
    char menu_01, menu_02;
    double total = 0, num1 = 0, num2 = 0;
    
    while( (menu_01 = displayMenu_01()) && menu_01 != 'q')
    {
        switch (menu_01)
        {
            case 'a':
            {
                int values;
                cout << " How many numbers would you like to enter? : ";
                cin >> values;
                float numbers[values];
                
                for(int counter=0; counter<values; counter++)
                {
                    cout << counter+1 << ") Enter number: ";
                    cin >> numbers[counter];
                }
                for(int counter=0; counter<values; counter++)
                    total+=numbers[counter];
                cout << "\n The Answer is : " << total << "\n\n ";
                break;
            }
                
            case 'b': // Subtraction Completed
                cout << " Enter first number: ";
                cin >> num1;
                cout << " Enter second number: ";
                cin >> num2;
                total=(num1-num2);
                cout << "\n The Answer is : " << total << "\n\n ";
                break;
                
            case 'c': // Divison Completed
                cout << " Enter first number: ";
                cin >> num1;
                cout << " Enter second number: ";
                cin >> num2;
                total=(num1/num2);
                cout << "\n The Answer is : " << total << "\n\n ";
                break;
                
            case 'd': // Multiplication
                cout << " Enter first number: ";
                cin >> num1;
                cout << " Enter second number: ";
                cin >> num2;
                total=(num1*num2);
                cout << "\n The Answer is : " << total << "\n\n ";
                break;
                
            case 'e':
            {
                menu_02 = displayMenu_02(); // another while is required here
                switch(menu_02)
                {
                    case 'a':
                        cout << "Pls enter no. to be squared: ";
                        cin >> num1;
                        cout << "x^2 = " << num1 * num1 << '\n';
                        break;
                    case 'm':
                        break;
                }
            }
        }
    }
    
    cout << " Thank you for using the Calculator\n\n ";
    
    return 0;
}
Last edited on
closed account (48T7M4Gy)
See this for the reason the braces I referred to are needed. https://stackoverflow.com/questions/5685471/error-jump-to-case-label
Thanks a lot, but i still have no idea how to do the powers and roots menu and also the option to ask the user if he wants "integer" or "floating point" before every computation. Pls help. 😭
closed account (48T7M4Gy)
You'll need to add another menu. So now you can have menu1, menu2 and follow the pattern you already have. ie case e) is print a new menu and a new loop to collect the choice and respond accordingly etc etc

POWERS & ROOTS MENU
---------------------------------------------------
a. Square a number (Xˆ2)
b. Cube a number (Xˆ3)
c. Raise to any power (XˆY)
d. Square root a number (Xˆ1/2)
e. Cube root a number (Xˆ1/3)
f. Take any root (Xˆ1/Y)
m. Return to Main Menu
q. Quit the program


Start simple - the first one, take the input number and multiply it by itself.
eg enter num1 as the number 2, so return num1*num1;

- cube is just multiplying num1 by itself 3 times.
- there are alternatives and complexities you can dream up here but you can use the pow() function mentioned above. You need to #include <cmath>
- sqrt(num1) function or pow(num1, 0.5) ...

That's pretty much it of how to do it. Maybe do a bit of googling or look at the tutorials here too for any missing bits and pieces.
I would do it like this...

start
do you want float or int?
read that in.
what are the numbers?
read the numbers. (type matters, use the if want float else want int logic here)
what do you want to do to them?
read action, perform computation, give result
return to start (or exit)




hey kemort it will absolutely wonderful if you show me the code for the new menu. I'll appreciate it alot.
closed account (48T7M4Gy)
@Sammilee77,

I've modified the code above to show you one way of incorporating the second menu and how it can work. It is only a start and you will have to fill it out. I know you can do it. Post any code you have written if there are any questions.

As I have shown you need a while loop around the first menu section and you'll need another one on the second menu section following the same pattern.

closed account (48T7M4Gy)
PS Hint: to 'q' quit you might think of an exit function.
@kemort, thanks for your help, This is what my code looks like now.... Thoughts?

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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

#include <iostream>
#include <cctype>
#include <stdlib.h>
#include <math.h>  

using namespace std;
//prototypes go here

char displayMenu_01();
char displayMenu_02();
void Get_x_and_y();
void Add_2_Numbers();
void Subtract_2_Numbers();
void Multiply_2_Numbers();
void Divide_2_Numbers();
void Square_A_Number();
void Cube_A_Number();
void Raise_to_any_power();
void Square_root_a_number();
void Cube_root_a_number();
void Take_any_Root();

float x, y, sum;



int main()                         // This function is used to call other functions.
{
	char choice;

	bool test = true;

	while (test)
	{
		choice = displayMenu_01();
		switch (choice)
		{
		case 'a': Add_2_Numbers();
			break;
		case 'b': Subtract_2_Numbers();
			break;
		case 'c': Divide_2_Numbers();
			break;
		case 'd': Multiply_2_Numbers();
			break;

		case 'e': 
			if (displayMenu_02() == 'q') test = false;
			break;
	
		case 'q': //Exit_the_program
			test = false;
			break;


	
		}


	}
	cout << "Thanks For Using calculator"<< endl;
	return 0;

}






char displayMenu_01()
{
	char choice;
	
	{
		cout
			<< " \n Hello, and welcome to the Calculator Practice Program."
			<< "    This program allows you to practice your math skills."
			<< "Choose what you want to practice in the menu shown below.\n\n"

			<< " (a) Addition\n"
			<< " (b) Subtraction\n"
			<< " (c) Division\n"
			<< " (d) Mulpilication\n"
			<< " (e) Powers & Roots\n"
			<< " (q) Exit\n\n"
			<< " Enter Your Choice[a-e,q ]: ";
		cin >> choice;
		//return choice;
	}

	// We now have the user's input in "choice".
	// We are going to analyze the input with decision logic.

	choice = tolower(choice);
	if (choice >= 'a' && choice <= 'e' || choice == 'q')
		cout << "You entered: '" << choice << "', that is correct input. " << endl;

	else
	{
		cout << "You did not enter 'a', 'b', 'c', 'e' or 'q'" << endl;
		//cout << "Please rerun the program." << endl;
	}

	cout << endl;
	return choice; 
}


char displayMenu_02()
{
	char choice;

	
	
	{
		//char choice;
		bool test = true;
		while (test)
		{
			cout
				<< "---------------------------------------------------\n"
				<< "POWERS & ROOTS MENU\n"
				<< "---------------------------------------------------\n"
				<< "a. Square a number (Xˆ2)\n"
				<< "b. Cube a number (Xˆ3)\n"
				<< "c. Raise to any power (XˆY)\n"
				<< "d. Square root a number (Xˆ1/2)\n"
				<< "e. Cube root a number (Xˆ1/3)\n"
				<< "f. Take any root (Xˆ1/Y)\n"
				<< "m. Return to Main Menu\n"
				<< "q. Quit the program\n"
				<< "---------------------------------------------------\n"
				<< "Enter your choice [ a - f, m, q ]:\n"
				<< "---------------------------------------------------\n"; 

			cin >> choice;

			switch (choice)
			{
			case 'a': Square_A_Number();
				break;
			case 'b': Cube_A_Number();
				break;
			case 'c': Raise_to_any_power();
				break;
			case 'd': Square_root_a_number();
				break;
			case 'e': Cube_root_a_number();
				break;
			case 'f':  Take_any_Root();
				break;
			case 'm':
				if (choice == 'm' || 'q') test = false; // m returns to main menu 
				break;
			case 'q':
				test = false;      // quits the program. 
				break;
			
			}

		}
	}
	return choice;
}





void Get_x_and_y()
{
	char choice;

	cout << "Will this be an Integer or floating point? " << endl;
	cout << "i   for  int " << endl;
	cout << "f for  float" << endl;
	cin >> choice; 
	
	choice = tolower(choice);

	switch (choice)
	{
	case 'i': cout << "Input values for x and y" << endl;
		cin >> x >> y;
		x = (int)x;
		y = (int)y;
		break;

	case 'f': cout << "input values for x and y"<< endl;
		cin >> x >> y;
		break;

	default: cout << "Input is incorrect, Please try again" << endl;
		break;
	}
	
	/*cout << "Please input x and y:   ";
	cin >> x;
	cin >> y;
	*/
}

void Add_2_Numbers()
{
	//int x, y, sum;		//first try it with "int"
	//float x, y, sum;//next try it with "float"
	cout << "This program will add x and y ..." << endl;
	cout << endl << endl;
	
	Get_x_and_y();


	sum = x + y;

	cout << endl << endl;
	cout << "  x + y = " << x << " + " << y
		<< " = " << sum << endl;

}


void Subtract_2_Numbers()
{
	//int x, y, sum;		//first try it with "int"
	float x, y, subtract; //next try it with "float"
cout << "This program will subtract, x and y ..." << endl;
cout << endl << endl;
cout << "Please input x and y:   ";
cin >> x;
cin >> y;

subtract = x - y;

cout << endl << endl;
cout << "  x - y = " << x << " - " << y
<< " = " << subtract << endl;


}



void Multiply_2_Numbers()
{
	//int x, y, sum;		//first try it with "int"
	float x, y, Multiply; //next try it with "float"
	cout << "This program will subtract, x and y ..." << endl;
	cout << endl << endl;
	cout << "Please input x and y:   ";
	cin >> x;
	cin >> y;

	Multiply = x * y;

	cout << endl << endl;
	cout << "  x * y = " << x << " * " << y
		<< " = " << Multiply << endl;

}

{
	//int x, y, sum;		//first try it with "int"
	float x, y, Division; //next try it with "float"
	cout << "This program will subtract, x and y ..." << endl;
	cout << endl << endl;
	cout << "Please input x and y:   ";
	cin >> x;
	cin >> y;


	Division = x / y;

	cout << endl << endl;
	cout << "  x / y = " << x << " / " << y
		<< " = " << Division << endl;
}


void Square_A_Number()
{
	float x, Square;
	cout << "This program will Square x.." << endl;
	cout << endl << endl;
	cout << "Please input x.... ";
	cin >> x ;

	Square = pow(x, 2.0);
	cout << endl << endl;

	cout << Square << endl;
}


void Cube_A_Number()
{
	float x, Cube;
	cout << "This program will Cube x.." << endl;
	cout << endl << endl;
	cout << "Please input x.... ";
	cin >> x ;

	Cube = pow(x, 3.0);
	cout << endl << endl;

	cout << Cube << endl;
}

void Raise_to_any_power()
{
	float x, y, Power;
	cout << "This program will raise x to any power.." << endl;
	cout << endl << endl;
	cout << "Please input x and y.... ";
	cin >> x;
	cin >> y; 

	Power = pow(x, y);
	cout << endl << endl;

	cout << Power << endl;
}


void Square_root_a_number()
{
	float x, Sqr_Root;
	cout << "This program will take the square root of a number.." << endl;
	cout << endl << endl;
	cout << "Please input x.... ";
	cin >> x;

	Sqr_Root = pow(x, 1.0 / 2.0);
	cout << endl << endl;

	cout << Sqr_Root << endl;
}


void Cube_root_a_number()
{
	float x, Cube_root; 
	cout << "This program will take the cube root of a number.." << endl;
	cout << endl << endl;
	cout << "Please input x.... ";
	cin >> x;

	Cube_root = pow(x, 1.0 / 3.0);
	cout << endl << endl;

	cout << Cube_root << endl;
}

void Take_any_Root()
{
	float x, Root;
	cout << "This program will take the cube root of a number.." << endl;
	cout << endl << endl;
	cout << "Please Input x and y.... ";
	cin >> x;
	cin >> y;

	Root = pow(x, 1.0 / y);
	cout << endl << endl;

	cout << Root << endl;
}


}
}
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

double Add_2_Numbers(double a, double b)
{
    return a + b;
}

int main()
{
    double total = 0.0;
    double num1 = 0.0, num2 = 0.0;
    
    cout << "Pls enter 1st no: ";
    cin >> num1;
    
    cout << "Pls enter 2nd no: ";
    cin >> num2;
    
    total = Add_2_Numbers( num1, num2);
    
    cout << total << '\n';
    cout << "Alternatively, total = " << Add_2_Numbers(num1,num2) << '\n';
    
    return 0;
}


This should be self-explanatory but the idea is you eneter the two numbers, they are 'sent' via a call to the function for processing inside the function and the function sends back the total of the two numbers.

The tutorials here might be useful to you :)

http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.