while(!(cin >> iLevel))
can be confusing at first.
what it is saying is "if iLevel is NOT an interger then keep looping".
because we have not assigned a value to iLevel yet, it will be true at least once and ask the user for input.
then if the user inputs something that is Non-int for example a string, the statement will be true again and ask the user to input value for iLevel and so on and so forth untill the user enters an integer.
lets say the person enters 109 as his level, then while(!(cin >> iLevel)) will be false, as 109 is an integer. so it will drop out of the loop into the second loop which checks if the level entered is within the range of 1-80.
because it is not it will ask him to re-enter the iLevel...
Do you get that so far? if so read on....
But lets say for example while in the while(range) he enters a string for iLevel, now we are back at problem one, but we are in the wrong loop to check for it...
this is where imbedded loops shine, if we need 2 or more conditions to hold true, then we can imbed loops within each other. eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
while(!(cin >> iLevel))
{
cout << "Input Wow level.\n";
cin.clear();
cin >> iLevel;
while (iLevel>80 || iLevel<1)
{
if (iLevel>80) {
cout << "Level entered is to HIGH! try again..." << endl;
}
else if (iLevel<1) {
cout << "Level entered is too LOW! try again..." << endl;
}
cout << "Enter real level:" << endl;
cin >> clear();
cin >> iLevel;
}
}
|