I'm a student in an intro C++ computer science course, and this is the first time I've posted here. We've just learned about while loops, and though the assignment doesn't require it, I'm trying to do input validation on this assignment. The program is meant to read a list of numbers and figure out the positions in that list where the first and last 8 are. So if I have a list of four numbers (1, 8, 42, 8), then the first and last 8 positions are 2 and 4. The size of the set is determined by the user.
I was trying to make a while loop that tested to be sure that what the user entered was actually a digit, but when I try entering in something like "." or "a" the loop goes on infinitely and doesn't terminate. I can't find my error, and as far as I can tell I'm using exactly the same syntax as what's in my textbook. Can someone show me what's wrong with my while loop?
int numbers, //How large the set will be
num, //What the user enters for each number
first8position = 0, //The first position in the set that has an 8
last8position = 0; //The last position in the set that has an 8
//Prompt the user to get set size
cout << "How many numbers will be entered? ";
cin >> numbers;
//Loop to get all the numbers of the set and figure out
//which position the first and last 8 are in
for (int position = 1; position <= numbers; position++)
{
cout << "Enter num: ";
cin >> num;
//If num isn't a digit, prompt the user to enter a digit
while (!isdigit(num))
{
cout << "Please enter a decimal number: ";
cin >> num;
}
//If num is 8, and first8position still isn't filled,
//set first8position to the current position.
//Otherwise, set last8position to the current position.
if (num == 8)
{
if (first8position == 0)
first8position = position;
else
last8position = position;
}
}
//If the set had an 8, print what its position was
if (first8position != 0)
cout << "The first 8 was in position " << first8position << endl;
//If there was more than one 8, print the last 8 position.
//Otherwise, the first and last 8 position are the same.
if (last8position != 0)
cout << "The last 8 was in position " << last8position << endl;
else
cout << "The last 8 was in position " << first8position << endl;
//If there were no 8s, say so.
if (first8position == 0)
cout << "Sorry, no eights were entered.";
return 0;
}