tags:

views:

161

answers:

3

I'm having trouble with isdigit. I read the documentation, but when I cout << isdigit(9), I get a 0. Shouldn't I get a 1?

#include <iostream>
#include <cctype>
#include "Point.h"

int main()
{
    std::cout << isdigit(9) << isdigit(1.2) << isdigit('c');
    // create <int>i and <double>j Points
    Point<int> i(5, 4);
    Point<double> *j = new Point<double> (5.2, 3.3);

    // display i and j
    std::cout << "Point i (5, 4): " << i << '\n';
    std::cout << "Point j (5.2, 3.3): " << *j << '\n';

    // Note: need to use explicit declaration for classes
    Point<int> k;
    std::cout << "Enter Point data (e.g. number, enter, number, enter): " << '\n' 
        << "If data is valid for point, will print out new point. If not, will not "
        << "print out anything.";
    std::cin >> k;
    std::cout << k;

    delete j;
}
+14  A: 

isdigit() is for testing whether a character is a digit character.

If you called it as isdigit('9'), it would return nonzero.

In the ASCII character set (which you are likely using), 9 represents the horizontal tab, which is not a digit.


Since you are using the I/O streams for input, you don't need to use isdigit() to validate the input. The extraction (i.e., the std::cin >> k) will fail if the data read from the stream is not valid, so if you are expecting to read an int and the user enters "asdf" then the extraction will fail.

If the extraction fails, then the fail bit on the stream will be set. You can test for this and handle the error:

std::cin >> k;
if (std::cin)
{ 
    // extraction succeeded; use the k
}
else
{
    // extraction failed; do error handling
}

Note that the extraction itself also returns the stream, so you can shorten the first two lines to be simply:

if (std::cin >> k)

and the result will be the same.

James McNellis
Didn't mean to steal your answer. I was off googleing and didn't see the reply.
Caleb Thompson
`isdigit`, not `isDigit` </pedant> ;)
tzaman
So how would I verify the userInput since I need the single quotes in the parantheses?
Crystal
@tzaman: Oops; shift-fail. :-) Corrected, and thanks.
James McNellis
Just a small question (C++ newb), is there something special about streams that allow them to reduce to a Boolean expression?
dreamlax
@dreamlax: yes -- they include a conversion function that allows them to be evaluated in a boolean context (though it's `operator void *` rather than `operator bool`).
Jerry Coffin
A: 

isdigit() works on characters, not ascii values, which you are currently passing. Try using isdigit('9').

Caleb Thompson
The function is `isdigit` (lowercase 'd'), arguably, `isdigit` may very well work on ASCII values.
dreamlax
Fixed that. It would work just fine on ASCII, but you'd have to know that you were using ASCII and not passing a character. I think he was trying to test '9' not `TAB`
Caleb Thompson
+5  A: 

isdigit() takes an int which is the representation of the character. Character 9 is (assuming you're using ASCII) the TAB character. Character 0x39 or '9' (not 9) is the actual character representing the digit 9.

The digit characters are integer codes 0x30 through 0x39 (or 48 through 57) in ASCII - I reiterate that since ASCII is not a requirement of the ISO C standard. Hence the following code:

if ((c >= 0x30) && (c <= 0x39))

which I've seen before, is not a good idea for portability since there is at least one implementation that uses EBCDIC under the covers - isdigit is the best option in all situations.

paxdiablo
+1 for noting the correct function signature, demonstrating the interchangability of `char` and `int`.
Steve Guidi
dreamlax
IIRC `isdigit('๑')` could also return true - char can also be a superset of ASCI.
MSalters