Help with calculator

How would i make it so where if the person enters 1 the program ends?
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
#include <iostream>

using namespace std;

int main()
{
    char operation;
    double number;
    double number2;
    double total;

    cout << "This is a calculator where you have to enter the problem in the order you want it solved. \n";
    cout << "Enter the first number in the problem: ";
    cin >> number;
    cout << "\n Now enter the operation: ";
    cin >> operation;
    cout << "\n Now enter the second number: ";
    cin >> number2;

    switch(operation){
        case '+':
        total = number + number2;
        cout << " \nYour current total is: " << total;
        break;

        case '-':
        total = number - number2;
        cout << " \nYour current total is: " << total;
        break;

        case '*':
        total = number * number2;
        cout << " \nYour current total is: " << total;
        break;

        case '/':
        total = number / number2;
        cout << " \nYour current total is: " << total;
        break;
    }

    label:

    cout << " \n Now enter the next operation or enter 1 if your problem is already solved: ";
    cin >> operation;
    cout << "\n Now enter the next number: ";
    cin >> number;

    switch(operation){
        case '+':
        total = total + number;
        cout << " \nYour current total is: " << total;
        break;

        case '-':
        total = total - number;
        cout << " \nYour current total is: " << total;
        break;

        case '*':
        total = total * number;
        cout << " \nYour current total is: " << total;
        break;

        case '/':
        total = total / number;
        cout << " \nYour current total is: " << total;
        break;
    }
    goto label;

    return 0;
}
Last edited on
Add another case label for '1' between lines 68 and 69?

Also, use a loop structure instead of goto.

There is a simpler way to do this with loops that does not involve duplicating code as you have done ;)
Last edited on
Topic archived. No new replies allowed.