Thread: check if input is int float or char

  1. #1
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16

    check if input is int float or char

    Hi All,

    is there any way by which we can determine whether the value keyed in by the user is char, float or int?

    Thanks in advance.

    regards,
    Ankit

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Read the line of input as a string.
    Look through it char by char.
    If they are all isdigit(ch), then it's an int (if it's in range).
    If they are all isdigit(ch) but for a single '.', then it's a float. (You can extend that to allow for 'e' notation.)
    If there are any other characters, it's a string of chars.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    If you scan it in you can create a program to determine that, but otherwise all input is ASCII code and you determine what to do with it.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you mean string, float, or int. The "value keyed in by the user" would begin as a string; you want to decide if this string is just a number, and if it is a number, if it is a whole number.

    C has some standard library functions for converting a string to an integer -- strtol() -- or a float -- strtof(). Unfortunately, you cannot use those in order, because they convert the initial part of a string, so if the input was 123.5 and you try strtol() first, it will succeed and return 123. If the input was just 123 and you try strtof() first, it will return 123.0. Also, both functions will succeed for "23skidoo", which is a string, not a number.

    So you have to decide first before you use a conversion function. If you work through the string one character at a time, you can check the ascii value of each char:

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    The set of characters that would constitute a string representation of float in C is greater than those that would constitute an integer, because it includes '.' and 'e' or 'E' for scientific notation.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by MK27 View Post
    C has some standard library functions for converting a string to an integer -- strtol() -- or a float -- strtof(). Unfortunately, you cannot use those in order, because they convert the initial part of a string, so if the input was 123.5 and you try strtol() first, it will succeed and return 123. If the input was just 123 and you try strtof() first, it will return 123.0. Also, both functions will succeed for "23skidoo", which is a string, not a number.
    You can if you use the end pointer argument, for example:

    Code:
    char *endp;
    int a;
    
    a = (int)strtol(argv[1], &endp, 0);
    if( endp != (argv[1] + strlen(argv[1])) ) {
        // strtol did not read the entire string
    }
    But I still think it's better to check character by character in this case since strtof would accept integers as well as floats.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by MK27 View Post
    C has some standard library functions for converting a string to an integer -- strtol() -- or a float -- strtof(). Unfortunately, you cannot use those in order, because they convert the initial part of a string, so if the input was 123.5 and you try strtol() first, it will succeed and return 123. If the input was just 123 and you try strtof() first, it will return 123.0. Also, both functions will succeed for "23skidoo", which is a string, not a number.
    They actually work fine if you examine the value returned in endptr. From the man pages
    Quote Originally Posted by man strtol
    SYNOPSIS
    #include <stdlib.h>

    long int strtol(const char *nptr, char **endptr, int base);
    long long int strtoll(const char *nptr, char **endptr, int base);
    ...
    If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns
    0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.
    ...
    RETURN VALUE
    The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow occurs, strtol()
    returns LONG_MAX. In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX).

    Quote Originally Posted by man strtof
    SYNOPSIS
    #include <stdlib.h>


    double strtod(const char *nptr, char **endptr);
    float strtof(const char *nptr, char **endptr);
    long double strtold(const char *nptr, char **endptr);
    ...
    RETURN VALUE
    These functions return the converted value, if any.


    If endptr is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr.


    If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.


    If the correct value would cause overflow, plus or minus HUGE_VAL (HUGE_VALF, HUGE_VALL) is returned (according to the sign of the value), and ERANGE is stored in errno. If the correct value
    would cause underflow, zero is returned and ERANGE is stored in errno.
    The way you handle this is by checking the contents of endptr. If it's a null character, you got to the end of the string:
    Code:
    char *endptr;
    long long_value;
    
    errno = 0;  // clear errno
    long_value = strtol(input, &endptr, 10);  // try to convert input as base 10
    if (errno == ERANGE || *endptr != '\0')=
        // value was out of range, or there was extra characters at the end of input, not a valid integer
    The code for strtof et al is very similar.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Subsonics View Post
    But I still think it's better to check character by character in this case since strtof would accept integers as well as floats.
    Nah, you just check the strictest case first. If strtol succeeds, then it's an integer. Else, if strtof succeeds, it's a float. Else it's a string.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by anduril462 View Post
    Nah,
    It's just your opinion.

  9. #9
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    Quote Originally Posted by oogabooga View Post
    Read the line of input as a string.
    Look through it char by char.
    If they are all isdigit(ch), then it's an int (if it's in range).
    If they are all isdigit(ch) but for a single '.', then it's a float. (You can extend that to allow for 'e' notation.)
    If there are any other characters, it's a string of chars.
    Hi,

    is isdigit(ch) function a system defined function?

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by ankiit View Post
    Hi,

    is isdigit(ch) function a system defined function?
    #include<ctype.h>

  11. #11
    Registered User ankiit's Avatar
    Join Date
    Jan 2012
    Location
    India
    Posts
    16
    yes i mean string. ..

    can you please show an example of the sample code.

    thanks
    ankit

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ankiit View Post
    yes i mean string. ..

    can you please show an example of the sample code.

    thanks
    ankit
    You've been given several possible solutions, including some examples. Try them out. When you get stuck, post your code, explain what is wrong in detail, and post your code. We will help you, but not write it for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char * to Float
    By kovitch in forum C Programming
    Replies: 5
    Last Post: 06-02-2011, 11:47 AM
  2. Replies: 5
    Last Post: 10-28-2009, 03:10 PM
  3. Check if user inserted value in a TextBox is a float
    By jmarcos in forum Windows Programming
    Replies: 6
    Last Post: 09-08-2009, 01:04 PM
  4. float input
    By Asimghouri in forum C Programming
    Replies: 3
    Last Post: 01-13-2009, 03:06 PM
  5. Float to char *
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2002, 08:11 AM