As far as isnumber() goes, I made a mistake. Apparently isnumber() is also a function because it didn't raise an error, but it was a typo -- the function I meant to use was isdigit(), not isnumber(). So, I changed the function isnumber() to isdigit(), but still got the exact same result.
I've since figured out what the problem is, though. The function isdigit() was given in the notes my professor provided, along with isalpha(), ispunct(), isspace(), isupper(), and islower(). It occurred to me that these functions are intended to be used with single characters, and in my code I had defined the variable
input as an
int. So, I changed
input to data type
char and it worked fine.
So, that solves that issue, but raises another. Consider the following code, if you will:
int numHands;
while (numHands < 1 || numHands > 7)
{
cout << endl << "How many hands do you want to play: ";
cin >> numHands;
if (numHands < 1)
{
cout << endl << "You must play at least one hand...";
wait();
}
else if (numHands > 7)
{
cout << endl << "You can not play more than seven hands...";
wait();
}
}
This is a simple bit of error checking to make sure that the number that the user enters is between 1 and 7, and in that regard, it works fine. It does not, however, ensure that what the user enters is a number in the first place. My original solution to this was the following code:
while (numHands < 1 || numHands > 7 || isdigit(numHands) == 0)
{
cout << endl << "How many hands do you want to play: ";
cin >> numHands;
if (numHands < 1)
{
cout << endl << "You must play at least one hand...";
}
else if (numHands > 7)
{
cout << endl << "You can not play more than seven hands...";
}
else if (isdigit(numHands) == 0)
{
cout << endl << "You did not enter a number...";
}
}
When this didn't work, it prompted me to post this thread, but as I mentioned at the beginning of this post, I realized that the reason it didn't work was because isdigit() works with
char and not
int data types, and I obviously can't declare
numHands as a
char because the user could enter more than one character as input.
How, then, can I error check to ensure that the user's input is, in fact, a number?