C Quirk??
Eddie Wyatt
edw at IUS1.CS.CMU.EDU
Fri Feb 5 06:45:01 AEST 1988
Not that this requires a net response, I just like reading my posts :-)
>
> #include <stdio.h>
> #include <ctype.h>
>
> main()
> {
> char ch;
>
> puts("Enter a Character: ");
>
> while( (ch = getchar()) )
> {
> if( isupper(ch) )
> tolower(ch);
>
> printf("%c\n", ch);
> puts("Enter a Character: ");
> }
> }
You must also read the carriage return. When you type a<return>
There are two character out there for you to read, not just one
as you might have thought - "a\n".
There are a couple of approaches to deal with this. One OS
depended would be to read characters as soon as they are typed,
hence not requiring the user to type a return. Another probably
more applicatable, would be to clear the input line after reading.
#define clearline() while (getchar() != '\n')
.
.
.
ch = getchar();
clearline();
.
.
.
--
Eddie Wyatt e-mail: edw at ius1.cs.cmu.edu
More information about the Comp.lang.c
mailing list