views:

1432

answers:

7

Hi

I am taking a string input from user. But how d I check whether user has entered a string or a number??

+2  A: 

You can check if its a digit:

char c;
scanf( "%c", &c );
if( isdigit(c) )
  printf( "You entered the digit %c\n", c );
optixx
See also: isalpha, ispunct, islower, isupper, etc... http://www.cppreference.com/wiki/c/string/start
Eclipse
Using scanf (or fscanf) for input validation is not a good idea, it opens up a too high risk of creating security errors. It _can_ theoretically be done safely ("%c" should be), but it is just much better to avoid scanf all together. Useful info at http://www.atsec.com/downloads/pdf/secure-coding-guidelines.pdf.
hlovdal
+1  A: 

You could use atoi or atof and then check if the value returned by this function is not 0. This is not infallible as the user could have entered '0'.

Another way would be to check that every character in the string is either [0-9], +/- or the decimal point and reject every string that doesn't conform.

Timo Geusch
atoi returns non-zero for the string "23 skidoo", so is not recommended for validating input.
Steve Jessop
Oops, that's not good. I don't write much C these days so that's escaped me...
Timo Geusch
+2  A: 
int isNum;
 if ((isNum = strtol(string, NULL, 10)))
  printf(" '%d' numbers was found!\n", isNum);
else
 {
     printf("No number found");
 }

All valid chars read are converted, if the string starts with an invalid character the function returns ZERO (0).

strtol() example that shows how to print out following characters after numbers or the part of the string thats not numbers.

or

 int isNum
 isNum = atoi(string);
TStamper
Do you mean isdigit(string[i])? Otherwise you're only accepting strings representing numbers between "48" and "57".
Eclipse
updated with better anwser
TStamper
you still aren't using the proper way to check if strtol succeeded or not. The proper way is if *nptr is not '\0' but **endptr is '\0' on return.
Evan Teran
@Evan-Since you referred to variables that are not even in my algorithm I was confused, I think you are talking about if they want to print out the characters in the string that isn't numbers, but the thing about that is if there are numbers after the characters then they will be printed out also. So it goes against saying no numbers if you see numbers
TStamper
+7  A: 

Call strtol, check that the value stored to endptr is not equal to the input (successful conversion), and is a pointer to a NUL byte (the whole string was used).

http://www.opengroup.org/onlinepubs/000095399/functions/strtol.html

explains that if you also want to detect overflow, the trick is to set errno to 0, then call strtol, then check that errno is still 0.

If you want to be pedantic, you also have to check using isspace() that the first character of the input string is a non-space. The reason is that strtol and friends skip over initial whitespace, but perhaps you don't consider " 1" to be a valid number.

Alternatives include strtoll and strtod, respectively if you want to allow bigger numbers, or non-integer numbers.

Steve Jessop
+1  A: 

You have not specified what 'numbers' you expect. Depending on the valid set of inputs, you can call a number of standard library functions as others have specified. For example: A simple check if it is a digit does not work if you are expecting floating point numbers. Also note that it is not enough to call the library functions but you also need to check for the return values (i.e. errors if any that occurred during the conversion).

Alternatively, you can use the sscanf function to retrieve the numbers.

Finally, an integer can be stored either as a int or variants thereof (long etc) or a floating point number. The call is yours.

dirkgently
+2  A: 

Lots of good heuristics here already.

If you want to define the input closely, you may want to write your own lexer/parser to accept exactly the language you mean rather that trying to build a makeshift out of standard library functions.

This is a substantial topic in and of itself. See one of the may "How to build a compiler?" questions on StackOverflow for references...(e.g. Learning to write a compiler).

dmckee
A: 

Instead of using functions like strtol, atoi, atof .. You could define your own function which has parses character by character of the string and builds the output integer. In case during this parsing if a character is a non-digit then you can safely say that the string in NaN. Or else you could also use isdigit() and check for each character and if all the characters are digits then convert the string to number using atoi.

Thunderboltz