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:
#include <iostream>
usingnamespace std;
int main()
{
int selectOne;
constint One = 1;
constint 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;
}
elseif ( 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.
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
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'.