Calculator Program Issue - ALMOST WORKING

Feb 14, 2013 at 5:45am
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:
...




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
// 
// Calculator Program

#include <iostream>
#include <cmath>
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;

}
Feb 14, 2013 at 5:51am
1) Delete semicolon after last else.

2)
1
2
3
4
5
6
7
8
9
while(true) {
    if (ans == 1) {
    //...
    } else if (ans == 2) {
    //...
    //...
    } else 
        break;
}
Feb 14, 2013 at 6:08am
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.
Feb 14, 2013 at 6:42am
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:
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
#include <iostream>
#include <cmath>
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 Feb 14, 2013 at 7:03am
Feb 14, 2013 at 7:10am
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).


Feb 14, 2013 at 7:29am
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.