Calculator not working as expected?

Hello, I've been updating my calculator a bit lately. Before I started to update it, it worked just fine, but now, it isn't working like before. It is compiling okay, but when I run the debugger, and type 'go' to start, like I want, nothing at all happens. I also made a function where the user can type 'help' for instructions, but now, that isnt even working, instead, it just closes the command prompt. Can someone please fix my code? It would be much appreciated! Here it is:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>

using namespace std;

int main()
{

    string start = "";

    double num1;
    double num2;
    double result;
    int a;

            while (a != 5)
{
    cout << "This calculator was made by \n" << endl;
    cout << "Type 'Go' to start the calculator, 'Help' for i"
         << "nstructions, or 'Exit' to quit\n" << endl;

    cin >> start;
    // Asks for inputs 'go' or 'help'
    cout << "\n" << endl;

    if (start == "exit")
    cout << "Press Enter to exit. \n";
    cin.ignore();
    cin.get();
    return 0;
    // Exits program if user imputs 'exit'
{
    if (start == "help")
    cout << "For steps 1 and 2, you will be asked to enter 2 nu"
         << "mbers.\nFor step 3, you will choose an operation, "
         << "for example,\nfor multiplication you would type '1"
         << "'. You will return\nto Step 1 after receiving the "
         << "answer to your calculation,\nand in order to exit,"
         << " you have to type '5' when you are\nat Step 3.\n"
         << endl;
}    // Text shown when 'help' is imput
{
    if (start == "go")
    // Below is what will happen if the user imputs 'go'

    cout << "Step 1" << endl;
    cout << "Enter first integer: \n" << endl;
    // Asks for first number

    cin >> num1;
    // Defines 'num1' as the number imput above

    cout << "\n" << endl;

    cout << "Step 2" << endl;
    cout << "Enter second integer: \n" << endl;
    // Asks for second number

    cin >> num2;
    // Defines 'num2' as the number imput above

    cout << "\n" << endl;

    cout << "Step 3" << endl;
    cout << "Choose an operation below (Type only the number)"
         << "\n\n1 = Multiply (x) ---- 5 = Exit calculator\n"
         << "2 = Divide   (/)           \n3 = Add      (+)\n4 = "
         << "Subtract (-)\n" << endl;
    // Asks for number of operation

    cin >> a;
    // Defines 'a' as the number imput above

    cout << "\n" << endl;

    switch(a)
{
    case 1:
        result = num1 * num2;
        cout << num1 << " x " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '1'. Selects multiplicat-
    // ion

    case 2:
        result = num1 / num2;
        cout << num1 << " / " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '2'. Selects division

    case 3:
        result = num1 + num2;
        cout << num1 << " + " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '3'. Selects addition

    case 4:
        result = num1 - num2;
        cout << num1 << " - " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '4'. Selects subtraction

    case 5:
        cout << "Thanks for using my calculator! Press Enter to"
             << "exit" << endl;
    // This case is used if user types '5'. Exits program
    cout << "Press enter to exit. \n";
    cin.ignore();
    cin.get();
    return 0;
}
}
}
}

Probably the syntax in your if statements.

You need to enclose any functionality that you want to be dependant on them inside braces.

After if(start=="exit") you don't have any braces. So the line following the if statement is dependant on it, everything else runs regardless.

Long story short, you're going to return 0 in every iteration of this loop as it stands.
Uhmm I really don't know what you're trying to say... I'm a beginner and I don't understand what you mean by functions being dependant, and which braces (open or closed) you are trying to tell me to use. Where exactly would I put braces?
As he said b4. For example :

if (start == "exit")
cout << "Press Enter to exit. \n";
cin.ignore();
cin.get();

since u are doing more than 1 action, U need to brace it.
This would work if u were just gonna make like this:

if (start == "exit")
cout << "Press Enter to exit. \n";

but since u got more than 1 action, U need to explain that cin.ignore(); and cin.get(); are running in the same if brace.

So the right syntax would be :

if (start == "exit") {
cout << "Press Enter to exit. \n";
cin.ignore();
cin.get();
}

Here is ur code rebuilded and working:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
#include <iostream>
#include <string>

using namespace std;

int main()
{

    string start = "";

    double num1;
    double num2;
    double result;
    int a;

            while (a != 5) {
                cout << "This calculator was made by \n" << endl;
                cout << "Type 'Go' to start the calculator, 'Help' for i"
                << "nstructions, or 'Exit' to quit\n" << endl;
                cin >> start;
                // Asks for inputs 'go' or 'help'
                cout << "\n" << endl;

                    if (start == "exit"){
                    cout << "Press Enter to exit. \n";
                    cin.ignore();
                    cin.get();
                    return 0;
                    // Exits program if user imputs 'exit'
                    }

    if (start == "help"){
    cout << "For steps 1 and 2, you will be asked to enter 2 nu"
         << "mbers.\nFor step 3, you will choose an operation, "
         << "for example,\nfor multiplication you would type '1"
         << "'. You will return\nto Step 1 after receiving the "
         << "answer to your calculation,\nand in order to exit,"
         << " you have to type '5' when you are\nat Step 3.\n"
         << endl;
}    // Text shown when 'help' is imput

    if (start == "go"){
    // Below is what will happen if the user imputs 'go'

    cout << "Step 1" << endl;
    cout << "Enter first integer: \n" << endl;
    // Asks for first number

    cin >> num1;
    // Defines 'num1' as the number imput above

    cout << "\n" << endl;

    cout << "Step 2" << endl;
    cout << "Enter second integer: \n" << endl;
    // Asks for second number

    cin >> num2;
    // Defines 'num2' as the number imput above

    cout << "\n" << endl;

    cout << "Step 3" << endl;
    cout << "Choose an operation below (Type only the number)"
         << "\n\n1 = Multiply (x) ---- 5 = Exit calculator\n"
         << "2 = Divide   (/)           \n3 = Add      (+)\n4 = "
         << "Subtract (-)\n" << endl;
    // Asks for number of operation

    cin >> a;
    // Defines 'a' as the number imput above

    cout << "\n" << endl;
    }

    switch(a)
{
    case 1:
        result = num1 * num2;
        cout << num1 << " x " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '1'. Selects multiplicat-
    // ion

    case 2:
        result = num1 / num2;
        cout << num1 << " / " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '2'. Selects division

    case 3:
        result = num1 + num2;
        cout << num1 << " + " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '3'. Selects addition

    case 4:
        result = num1 - num2;
        cout << num1 << " - " << num2 << " = " << result << "\n"
             << "";
        cout << "\n" << endl;
        cout << "When you're finished, type '5' at Step 3 to ex"
             << "it\n" << endl;
        break;
    // This case is used if user types '4'. Selects subtraction

    case 5:
        cout << "Thanks for using my calculator! Press Enter to"
             << "exit" << endl;
    // This case is used if user types '5'. Exits program
    cout << "Press enter to exit. \n";
    cin.ignore();
    cin.get();
    return 0;
}
}
}



I did not try it out to 100 % just checked that the program is running correctly. If u got more problems, Just ask.
It works, thanks much guys :) I get what you mean by the braces now iHutch
Topic archived. No new replies allowed.