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).

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.
-----------------------------------------------
-----------------------------------------------
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 ]:
-----------------------------------------------

Hello, and welcome to the Powers & Roots Menu.
This menu allows you to take powers and roots of a number.
---------------------------------------------------
---------------------------------------------------
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)
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 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"

<< " (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[])
{
float total, num1, num2;

system("CLS");

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

}
else
{
{
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:

int ai, bi, totali;
...
case d:
cin etc
totali = ai*bi;

...
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:
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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````#include #include 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> numbers[counter]; } for(int counter=0; counter> 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?
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

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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372`` `````` #include #include #include #include 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)
 ``123456789101112131415161718192021222324252627`` ``````#include 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.