### calculator

how can i do a program that will input a 4 different numbers and operators??..
Do you want to do the whole operation in one line, or just to be able to perform many operations in a row using the previous outcome?
i want to do the MDAS operation to this programs using c++
" Multiplication Division Addition Subtraction "
how can i be able to perform 4 different operations in a row, even though in different position of the operation,
Basically you have to use nested switches, I guess you can do something like:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````double num1, num2, num3 ... char operator1, operator2, operator3 ... double answer; int values; int maxvalues = //set this to how many values you can handle cout << "How many values would you like to use (at least 2) ? "; cin >> values; if (values < 2) { cout << "Invalid number of values } // You have to have at least 2 values else if (values > maxvalues) { cout << "Please use less values, this calculator is rather limited" } else if (values = 2) { cout << "Enter your first number: \n"; cin >> num1; cout << "Enter the operator (+, -, * or /): \n"; cin >> operator1; cout << "Enter your second number: \n"; cin >> num 2; switch(operator1) { case '+': num1 + num2 = answer; break; ... //do something like that for other operators } else if (values = 3) { cout << "Enter your first number: \n"; cin >> num1; cout << "Enter the first operator (+, -, * or /): \n"; cin >> operator1; cout << "Enter your second number: \n"; cin >> num 2; cout << "Enter the second operator (+, -, * or /): \n"; cin >> operator2; cout << "Enter your third number: \n"; cin >> num3; switch(operator1) { case '+': switch(operator2) { case '+': num1 + num2 + num3 = answer; break; ... } ... //do something like that for other operators } cout << "The answer is: " << answer << "\n"; //You can add a do while loop for bool again ``````

This is just the basic solution, but it's not very good and I'm sure you can improve it, I'll leave you at least a bit of work :)
Last edited on
You do realize that even if you code this "correctly" it will not calculate the right answers don't you? You need to consider operator precedence. If you just perform the operation as they are typed in the user will have to be the one to remember the proper order.

Your example: ( 7 + 2 - 6 * 4 / 3 ) = 4 if you ignore the precedence.

The correct way to solve this equation is
(7 + 2 - ((6 * 4) / 3))) =
(7 + 2 - (24 / 3)) =
(7 + 2 - 8) =
1.
Hah, thanks, I know. Everything works, you just have to add parentheses to the code, like so :

 ``12345678910111213141516171819202122232425262728293031323334`` ``````switch(operator1) { case '+': switch(operator2) { case '+': num1 + num2 + num3 = answer; break; // case '-': case '*': num1 + (num2 * num3) = answer; break; //same for division } break; ... //do something like that for other operators //Here's the example for multiplication: case '*': switch(operator2) { case '+': (num1 * num2) + num3 = answer; break; // case '-': case '*': (num1 * num2) + num3 = answer; break; //same for division } break; }``````
Last edited on
Topic archived. No new replies allowed.