Help with loops, and breaking it.

Alright, so what I want to do is make a program where it only stops if I hit a certain letter - in this case, 'e'.
I have everything figured out, except after adding the loop whenever I press 'e', it just continues to ask me the first line over and over.
I tried using break; before the input for 'e', but then the loop wouldn't work at all. Here's the code:

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

using namespace std;

int main()
{
    char input;
    double num1, num2, result;

    for(;;)
    {

    cout << "Hello, please give me an operation (+, -, *, /) to perform, or press 'e' to escape: " << endl;
    cin >> input;

   if (input == '+')
    {
        cout << "You selected +! Please enter the two operands: ";
        cin >> num1 >> num2;

        result = num1 + num2;

        cout << "The result is: " << result << endl;
    }

   if (input == '-')
    {
        cout << "You selected -! Please enter the two operands: ";
        cin >> num1 >> num2;

        result = num1 - num2;

        cout << "The result is: " << result << endl;
    }

   if (input == '*')
    {
        cout << "You selected *! Please enter the two operands: ";
        cin >> num1 >> num2;

        result = num1 * num2;

        cout << "The result is: " << result << endl;
    }

   if (input == '/')
    {
        cout << "You selected /! Please enter the two operands: ";
        cin >> num1 >> num2;

        result = num1 / num2;

        cout << "The result is: " << result << endl;
    }

break;

    }

   if (input == 'e')
    {
        cout << "Goodbye!" << endl;
    }

   return 0;
}


Help, please?
Put the break at line 63 and have the check for 'e' within the loop.

Hmm, but you never input num1 or num2?
Last edited on
@LowestOne: I tried putting the break where you said to, but then the program would loop at the point after I selected what kind of operation I want to do.

And you input num1 and num2 after you select which kind of operation you want to do.
The way it is written the user inputs data, and if that data doesn't match +, -, *, /, it leaves the loop. It then checks to see if input is equal to 'e' then terminates the program.

Look at where the braces are located and notice that the loop control structure terminates after the break; statement. The test to see if input == 'e' never occurs within the loop itself, only after it. try moving the brace that is directly below the break; command beneath the if{...} section that checks for input == 'e', and placing that break; statement within that decision structure that tests for the same.
Works for me...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//...
   if (input == '/')
    {
        cout << "You selected /! Please enter the two operands: ";
        cin >> num1 >> num2;

        result = num1 / num2;

        cout << "The result is: " << result << endl;
    }

   if (input == 'e')
    {
        cout << "Goodbye!" << endl;
break;
    }

    }
   return 0;
}
Last edited on
@progrady: I tried that, but then that makes the loop not happen at all. If I input 'e', it'll end the program. And if I input + and the two numbers, it won't ask me to pick what sort of operation I want to try again, it'll just stop.

Edit: I was placing the break; in the wrong spot. Thank you so much!
Last edited on
Topic archived. No new replies allowed.