< Previous | Contents | Next >
This function asks for a number within a range and keeps asking until the player enters a valid number. It receives a question, a high number, and a low number. It returns a number within the range specified.
int askNumber(string question, int high, int low)
{
int number; do
{
cout << question << " (" << low << " - " << high << "): "; cin >> number;
} while (number > high || number < low);
return number;
}
If you take a look at this function’s prototype, you can see that the low number has a default value of 0. I take advantage of this fact when I call the function later in the program.