[updated with new question @ post 13] Hello everyone
I'm sure you've all seen this before, but I can't find the solution to my problem.
I don't want any values accepted if they're not within 1 and 100. My problem is that if I enter ffadafdfghf55asdfasdf (or w/e), 55 is entered into the int and the loop becomes true.
I searched the nets which led me here, and to where I've found part of the solution below, however, I get the above stated problem
How do I make it so ONLY a number from 1 to 100 is accepted, and not something like dfg66, or the like?
The book I'm learning from doesn't cover this, and I know there has to be a solution! I'd like to not be able to break my own code!
Thank you for any help and/or insight as to whats happening
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a number between 1 and 100 and I will guess it: ";
unsigned short playerNumber;
cin >> playerNumber;
while(!(cin >> playerNumber) || playerNumber < 1 || playerNumber > 100)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Try again: ";
}
return 0;
}
|