Note that std::cin
will be in bad state if you enter values of unexpected type (such as string when int is expected). If a stream is in invalid state, then it will fail to read even valid input, and the loop will run infinitely. That seems to be your case, so you need to clear the stream and since the stream didn't the invalid input, you need to skip it (using ignore
function):
double alphaval; //it must be double or float (as per your requirement)
cin >> alphaval;
while (alphaval < 0 || alphaval > 1)
{
cout << "Invalid entry! Please enter a valid value: ";
//FIX
cin.clear(); //clear bad flags!
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //skip bad input
cin >> alphaval; //read fresh!
}
Include <limits>
header file for std::numeric_limits<>
.