problem with simple error checking

Scratch the error checking bit. The original code involved error checking, but I've dwindled the problem down to the following code:


int input;

cout << endl << "Enter number: ";
cin >> input;

if (isnumber(input) == 0)
{
cout << "Not a number";
}
else
{
cout << "Is a number";
}


How is it that, when I enter the number 2 as input for this program, I get the response "Not a number." I get the same response when I enter something that actually is not a number, like the character 'a', as input. No matter what, it displays "Not a number."

This seems really basic to me. Am I missing something obvious here?

MacBook, Mac OS X (10.5.8)

Posted on Sep 9, 2010 3:37 AM

Reply
10 replies

Sep 9, 2010 6:12 AM in response to etresoft

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?

Sep 9, 2010 6:34 AM in response to Tron55555

Did I mention that this was a mess?

The only real solution is to read in data a line at a time, as a string, and then parse it. I don't know if that solution is appropriate for an assignment. The ultimate goal of this assignment may be to teach you how to use the formatted input streams and to realize, at the end, how much of a mess it is. In that context, checking the stream's status may be the way to go, assuming you can use techniques from chapter 9 in week 1 of the class. Try something like:


cin >> input;
if(cin)
{
// input was an int.
If(input > 0)
{
// input is valid.
}
}

Sep 10, 2010 5:43 AM in response to etresoft

Okay. Interesting. I played around with that some. I tried incorporating it into my code, but after the first iteration I wasn't allowed to re-enter my input, so I gave up.

Anyways, out of curiosity, how come, in the code below, an error isn't raised when I attempt to store the value 'a' in an int variable?


int input;
cout << "Enter a number: ";
cin >> input;


If I enter a non-numerical value here, like 'a', how come I don't get a runtime error? I was thinking maybe its ASCII value was assigned to the int variable or something, but I can enter a whole string of characters and I still get no error. I found this surprising.

Sep 10, 2010 6:12 AM in response to Tron55555

Tron55555 wrote:
If I enter a non-numerical value here, like 'a', how come I don't get a runtime error? I was thinking maybe its ASCII value was assigned to the int variable or something, but I can enter a whole string of characters and I still get no error. I found this surprising.


That is by design. C++ was notoriously difficult to implement. Little companies like Sun and Intel just didn't have the time and resources to be able to create a standards-compliant C++ compiler. In the end, they wound up taking out some of the harder, albeit very useful, parts of the language in order to focus on bloated, mind-twisting parts.

What happened was that no one had support for exceptions yet, so the I/O streams, by default, don't throw exceptions for these errors. You can turn that on with the exceptions() member function of the std::stream classes.

Sep 10, 2010 6:25 AM in response to Tron55555

Okay, that makes sense. Thank you.

Also, I played with the code you gave with the if statement using cin as the condition, and bumped into an issue.


int input;
cout << "Enter a number: ";
cin >> input;
if (!cin)
{
cout << endl << "You did not enter a number."
<< endl << "Enter a number: ";
cin >> input; // problem here
}


The code works in the sense that if I enter a numerical value, the test fails and the program terminates as it should, and if I enter a non-numerical value, I get the error message just like I should. So that's all good. It works as far as testing whether or not a numerical value was entered goes. The problem comes with the next input statement inside the if structure. After the "You did not enter a number" error message, "Enter a number: " is displayed on the next line, like it should be, except the program terminates then and there. I can't enter any input after the "Enter a number: " line because the program terminates. It's as if the second +cin >> input;+ statement (the one inside the if structure) is being ignored all together. The program displays the two lines of text from the cout statement inside the if structure and immediately terminates. Can you offer any insight?

Sep 10, 2010 6:51 AM in response to Tron55555

Tron55555 wrote:
the program terminates then and there. I can't enter any input after the "Enter a number: " line because the program terminates. It's as if the second +cin >> input;+ statement (the one inside the if structure) is being ignored all together. The program displays the two lines of text from the cout statement inside the if structure and immediately terminates. Can you offer any insight?


Your stream is now in a failed state and it will stay that way until you clear the fail state with the clear() method.

If you are working heavily in C++, I strongly recommend "The C++ Standard Library: A Tutorial and reference" by Nicolai M. Josuttis.

Sep 10, 2010 7:16 AM in response to etresoft

I looked into the book you recommended. It looks like I could learn a lot from it, although it's a bit on the expensive side. Hopefully I'll be able to afford it sooner or later -- it looks good.

By the way, the clear() method you mentioned -- do I need to #include something for it to work, because I'm getting an error when I try to use it ("error: 'clear' was not declared in this scope")?

Sep 10, 2010 8:01 AM in response to Tron55555

Tron55555 wrote:
I looked into the book you recommended. It looks like I could learn a lot from it, although it's a bit on the expensive side. Hopefully I'll be able to afford it sooner or later -- it looks good.


I paid $50 for mine in 1999, back when I was still writing in C++. It is invaluable because it actually gives you examples on how to use the tricky and obscure parts of C++. It is particularly good information on I/O streams.

By the way, the clear() method you mentioned -- do I need to #include something for it to work, because I'm getting an error when I try to use it ("error: 'clear' was not declared in this scope")?


You should be able to do std::cin.clear();

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

problem with simple error checking

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.