Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getchar() reading alphanumeric data

Reply
Thread Tools

getchar() reading alphanumeric data

 
 
White Spirit
Guest
Posts: n/a
 
      02-11-2006
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?

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-11-2006
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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
alphanumeric data clean up Charles L. Snyder Ruby 4 10-28-2008 12:34 AM
Alphanumeric font dereklai2k@yahoo.com.hk Java 2 12-17-2004 12:23 PM
Check if var is alphanumeric? VB Programmer ASP .Net 2 11-16-2004 06:46 PM
Natural sorting order for alphanumeric fields Paul Java 1 09-14-2004 12:41 PM
Easy way to specify all non-alphanumeric characters? Steven J Sobol Java 8 04-30-2004 09:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57