### Calculator Program Issue - ALMOST WORKING

I'm only allowed to use certain basic concepts, which is why the code looks like it does.

The program is working, except after it computes the equation, I need the program to start all over. Do I have to include a loop?

Also: the way it's written now, when the answer is generated - it then automatically outputs "Cannot Initiate Calculator". This is only supposed to be Output if the user inputs a number outside of 1-4 (when selection an operation).

Here are my instructions

 Instructions: prints a numbered menu of mathematical operations as shown below; then prompts the user to select an operation; then prompts and inputs the operands for the selected option (note that some operations require one and some two operands); then computes and outputs the result. The process repeats. The program should quit if the user inputs an option (number) that is not listed. The dialog should look as follows: 1. absolute value 2. square root 3. ceiling 4. power Select an operation: 4 Enter base: 2 Enter exponent: 3 The result is: 8 Select an operation: ...

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````// // Calculator Program #include #include using namespace std; int main() { int ans, x; double a, b, y, z; cout << "1. Absolute Value " << endl; cout << "2. Square Root " << endl; cout << "3. Ceiling " << endl; cout << "4. Power " << endl; cout << "Select an operation: " << endl; cin >> ans; if (ans == 1) { cout << "Enter the number whose absolute value you want: " << endl; cin >> x; cout << "The absolute value is " << abs(x); } else if (ans == 2) { cout << "Enter the number whose square root you want: " << endl; cin >> y; cout << "The Square Root is " << sqrt(y); } else if (ans == 3) { cout << "Enter the number whose ceiling you want: " << endl; cin >> z; cout << "The Ceiling is " << ceil(z); } else if (ans == 4) { cout << "Enter base: " << endl; cin >> a; cout << "Enter Exponent: " << endl; cin >> b; cout << "The Result is: " << pow(a,b) << endl; } else; cout << "Cannot Initiate Calculator. Goodbye! "; return 0; }``````
1) Delete semicolon after last else.

2)
 ``123456789`` ``````while(true) { if (ans == 1) { //... } else if (ans == 2) { //... //... } else break; }``````
Thanks, fixed my second issue.

Can anyone advise me with repeating the program? Is there anyway to do this without writing a loop? I just want the program to start over (for the user) after it gives the answer.
I have posted example of relization of this:`The process repeats. The program should quit if the user inputs an option (number) that is not listed.` under (2)

Full code, if you want:
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include using namespace std; int main() { int ans, x; double a, b, y, z; while(true) { cout << endl; cout << "1. Absolute Value " << endl; cout << "2. Square Root " << endl; cout << "3. Ceiling " << endl; cout << "4. Power " << endl; cout << "Select an operation: " << endl; cin >> ans; if (ans == 1) { cout << "Enter the number whose absolute value you want: " << endl; cin >> x; cout << "The absolute value is " << abs(x); } else if (ans == 2) { cout << "Enter the number whose square root you want: " << endl; cin >> y; cout << "The Square Root is " << sqrt(y); } else if (ans == 3) { cout << "Enter the number whose ceiling you want: " << endl; cin >> z; cout << "The Ceiling is " << ceil(z); } else if (ans == 4) { cout << "Enter base: " << endl; cin >> a; cout << "Enter Exponent: " << endl; cin >> b; cout << "The Result is: " << pow(a,b) << endl; } else break; } cout << "Cannot Initiate Calculator. Goodbye!" << endl;; return 0; }``````
Last edited on
Thanks so much! This runs perfectly.

So what exactly made this work?

Adding the "While(True)" on line 9, along with the "break" at 36?

Also, why are there two semicolons on line 38? (This is simply for my understanding).

The while (true) loop will run indefinitely, so we will need some way of eclaping infinite loop: break will do it.

Double semicolons is just a copy-paste error here, sorry for that.
Topic archived. No new replies allowed.