when someone enters a letter when they are supoosed to enter a number :/

Status
Not open for further replies.

Garf

Registered Member
Hi, I am doing my first c++ app and it's an assignment that I am doing at university (college if you like) and I have an input validated so that a person can't enter a number <1 or > 9 but if they enter a letter it stuffs up the program...

I was wondering if there was a way for me to check if they entered a letter...I will post the code:

cout << "Player " << player << " Please enter where you would like you place your X or O\n";
cin >> gridNumber;
while (gridNumber < 1 || gridNumber > 9)
{
cout << "\n";
cout << "You have entered an invalid number\n";
cout << "Please enter a valid number\n";
cin >> gridNumber;
}

hope someone can help me soonish :)

thanks heaps in advance guys :)
 
I've never done c++, but I would check to see if it is a letter inside your while loop...

i.e. do a

while ((gridNumber < 1 || gridNumber > 9) && !(gridNumber == letter))

obviously changing letter with a proper c++ is it a letter check.

Bye
 
char input;
cin >> input;

if(input >= '0' && input <= '9')
cout << "number" << endl;
else if( (input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z') )
cout << "letter" << endl;
else
cout << "other" << endl;
 
Status
Not open for further replies.
Back
Top