Hi guys!
I am having a little problem with my latest game in c++
i know how to give an error if the user types in a wrong number,
but i dont know how to make it if the user types in a string!
Anyone can help?
This is the code of the error so far:
1 2 3 4 5 6 7 8 9 10 11 12
try
{
if (number < 1 || number > 9)
{
throw 1;
}
}
catch(int x)
{
cout << "\n You typed a wrong number! ERROR: " << x << endl;
game();
}
int nNumber;
std::cout << "Please enter a number: ";
std::cin >> nNumber;
// to check if something worked or not.
if(std::cin.good()) // this would check to see if anything else than the integer was entered.
{
// do your exception here....
}
else
{
std::cout << "you didn't enter a number!!" << std::endl;
}
You can find answers like this all over the board, it isn't the first time it has been asked.
I get the feeling you're not testing the code we have given you.
"3sadksdk" would be a string, not an integer. This would make cin.good() would return a false, and cin.fail() would return a true, since I couldn't read this into a integer type.
If I wanted to catch the number in the string, I would use a different approach.
You asked about a number, which would be an integer or float. What you gave was a number within a string. They are two different things and would be handled differently.
can you show me read in structure? and how you process it? I would assume it is on a loop of some form, if it show me that too. I figure it wouldn't be too long and won't reveal that much about your game.