getchar() reading alphanumeric data

Discussion in 'C Programming' started by White Spirit, Feb 11, 2006.

  1. White Spirit

    White Spirit Guest

    I'm trying to use getchar() to read alphanumeric data as follows:-

    char input[150];

    /* Take a string of input and remove all spaces therein */
    int j = 0;
    while ((input[j] = getchar()) != '\n')
    {
    if (!isspace(input[j]))
    j++;
    }

    /* Append the null character */
    input[j] = '\0';

    I'm aware there's no bounds checking at present - it's forms part of
    test code at present. The problem is, I get unexpected behaviour when
    reading digits. With Linux and Solaris, if the data starts with a digit
    the programme hangs. With Linux, if the stream begins with an alpha
    character, it works as intended but on the Solaris box I get entirely
    different characters.

    I've looked in books and on Google but nothing is specifically mentioned
    about this. I assume that getchar() is intended for alpha data only,
    which presumably means that using scanf would be better with a separate
    function to remove the whitespace?
     
    White Spirit, Feb 11, 2006
    #1
    1. Advertising

  2. White Spirit <> writes:
    > I'm trying to use getchar() to read alphanumeric data as follows:-
    >
    > char input[150];
    >
    > /* Take a string of input and remove all spaces therein */
    > int j = 0;
    > while ((input[j] = getchar()) != '\n')
    > {
    > if (!isspace(input[j]))
    > j++;
    > }
    >
    > /* Append the null character */
    > input[j] = '\0';
    >
    > I'm aware there's no bounds checking at present - it's forms part of
    > test code at present. The problem is, I get unexpected behaviour when
    > reading digits. With Linux and Solaris, if the data starts with a
    > digit the programme hangs. With Linux, if the stream begins with an
    > alpha character, it works as intended but on the Solaris box I get
    > entirely different characters.
    >
    > I've looked in books and on Google but nothing is specifically
    > mentioned about this. I assume that getchar() is intended for alpha
    > data only, which presumably means that using scanf would be better
    > with a separate function to remove the whitespace?


    getchar() reads a single character from standard input. It doesn't
    distinguish between alphanumeric characters and any other characters.

    It's difficult to tell what's going wrong with your program because
    you haven't shown us your program; you've only posted a code fragment.

    getchar() returns either a character value (as an unsigned char
    converted to int) *or* the special negative value EOF. You need to
    store the result of getchar() in an int, not in a char. You also need
    to check whether it returned EOF. In your code fragment, if you hit
    end-of-file before seeing a '\n', you'll have an infinite loop.

    Read section 12 of the C FAQ, <http://www.c-faq.com/>. If you're
    still having problems, post a small, complete, compilable program that
    illustrates whatever problems you're having. Be sure you have the
    required #include directives for whatever functions you're using
    (<stdio.h> for getchar(), <ctype.h> for isspace()).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Check if var is alphanumeric?

    VB Programmer, Nov 16, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    592
    bruce barker
    Nov 16, 2004
  2. Steven J Sobol
    Replies:
    8
    Views:
    5,741
    Thomas Weidenfeller
    Apr 30, 2004
  3. Paul
    Replies:
    1
    Views:
    13,320
    Rogan Dawes
    Sep 14, 2004
  4. Replies:
    19
    Views:
    725
  5. Charles L. Snyder

    alphanumeric data clean up

    Charles L. Snyder, Oct 25, 2008, in forum: Ruby
    Replies:
    4
    Views:
    103
    Charles L. Snyder
    Oct 28, 2008
Loading...

Share This Page