weird 'while' loop

I thought a knew a thing or two about while loops but I can't
honestly say why this one should be looping endlessly once it hits the 'else' statement within the while:


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

using namespace std;

int main()
{
    
    int selectOne;
    const int One = 1;
    const int Two = 2;


    bool endLoop = true;

    
    cout << "Enter a number: \n";
    cin >> selectOne;
    cin.get();

    while ( endLoop == true )
    {

          if ( selectOne == One )
          {
               cout << "number One" << endl;
               endLoop = false;
          }
          else if ( selectOne == Two )
          {
               cout << "number Two" << endl;
               endLoop = false;
          }
          else
          {
              cout << "Not the right number." << endl;
              cout << endLoop << endl;
          }

    }
          cout << "outside the loop!\n";

                
    return 0;

}



Any considerations would be appreciated. I'm wondering if its some bizarre syntax issue. I've written these before with not a problem. Maybe I'm just blind to something at the moment.

Thanks!
-Mark
Last edited on
You can use "break statement":

Hence 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
41
42
43
44
45
#include <iostream>

using namespace std;

int main()
{
    
    int selectOne;
    const int One = 1;
    const int Two = 2;


    bool endLoop = true;

    
    cout << "Enter a number: \n";
    cin >> selectOne;

    while ( endLoop == true )
    {

          if ( selectOne == One )
          {
               cout << "number One" << endl;
               break;
          }
          else if ( selectOne == Two )
          {
               cout << "number Two" << endl;
               break;
          }
          else
          {
              cout << "Not the right number." << endl;
              cout << endLoop << endl;
              break;
          }

    }
          cout << "outside the loop!\n";

                
    return 0;

}


And i dont see a point of using a while loop!
Last edited on
I basically want it to keep evaluating till I do type in the right value.
This is a simplified example of some other code I was trying. I figured if I broke it down to its bare parts I could see the problem. Apparently I couldn't
Move
1
2
3
cout << "Enter a number: \n";
cin >> selectOne;
cin.get();


to after the opening { of the while loop:

1
2
3
4
5
while ( endLoop == true )
{
    cout << "Enter a number: \n";
    cin >> selectOne;
    cin.get();


What happens is that you never ask the program to get another value from the user for "selectOne", so it maintains the initial value, which will always be wrong (say if you enter '3') every time it runs through the loop, because the user is never able to change the value to '1' or '2'.
Last edited on
Oh jeez. Haha.

I figured it was something obvious.
Thanks!

-Mark
Topic archived. No new replies allowed.