Link to home
Start Free TrialLog in
Avatar of team
team

asked on

getchar ??

i had written the below simple prog . however i am facing a prob .

the prog is

 #include<stdio.h>
 #include<string.h>
void main()
{
int i,j;
char num[10],ch='y';
while(ch=='y'||ch=='Y')
{
printf("\n enter the number ");
scanf("%s",num);
j=strlen(num);
for(i=0;i<=j;i++)
switch(num[i])
{
 case '0':printf(" zero "); break;
 case '1':printf(" one "); break;
.
.
.
.
case '9':printf(" nine ");
default: printf(" invalid number");
}
printf(" want to enter an other num");
ch=getchar();
}
}

the prob is that it is not waiting for me to enter a char after getchar it is directly going to the $ prompt. i think it is the buffer problem. how to clear the buffer. flushall does not work.how to do it with getchar. i then declared ch as string i.e array of 1 char. then it worked just fine.
i am working on sun os 5.6 c version 4.something
Avatar of team
team

ASKER

i am still going crazy
ASKER CERTIFIED SOLUTION
Avatar of bedot
bedot

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just add one more getchar() next to
  ch=getchar();
line.  This is because, getchar takes, your character for the first time input and then the ENTER key as the next character in the buffer, due to which the while loop fails.

Try the following code: It will work.


#include<stdio.h>
 #include<string.h>
void main()
{
int i,j;
char num[10],ch='y';
while(ch=='y'||ch=='Y')
{
printf("\n enter the number ");
scanf("%s",num);
j=strlen(num);
for(i=0;i<=j;i++)
switch(num[i])
{
 case '0':printf(" zero "); break;
 case '1':printf(" one "); break;
.
.
.
.
case '9':printf(" nine ");
default: printf(" invalid number");
}
printf(" want to enter an other num");
ch=getchar();
getchar();
}
}





Regards
Kannan.

All you need to do is add the line:
fflush(stdin);
after the scanf() statement.

hope this helps,
thanks,
pagladasu
while ((i=getchar())!='\n')
surely take care of \r (return) because we are here under unix and the I/O peripheral is  in  icrnl mode that maps inputing CR into NL  like this example:


root@med1091[/]stty -a
speed 9600 baud; line = 0;
rows = 24; columns = 80
min = 4; time = 0;
intr = ^C; quit = ^\; erase = ^H; kill = ^U
eof = ^D; eol = ^@; eol2 <undef>; swtch <undef>
stop = ^S; start = ^Q; susp <undef>; dsusp <undef>
werase <undef>; lnext <undef>
parenb -parodd cs7 -cstopb hupcl -cread -clocal -loblk -crts
-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc
ixon -ixany ixoff -imaxbel -rtsxoff -ctsxon -ienqak
isig icanon -iexten -xcase echo echoe echok -echonl -noflsh
-echoctl -echoprt -echoke -flusho -pendin
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel -tostop
root@med1091[/]

There is a logic error in your program.  You have the looping construct

   while( ch=='y'||ch=="y' )

which loops while the character is upper or lower case y.  This
loop will fail immediately because it gets the uninitialized value
of ch, which is probably NUL.

To fix the program, simply change the while to until and you'll be fine.
No need to go for something sophisticated like flushing the buffer.

Avatar of ozo
ch was initialized to 'y'
Well, I just tried it.  I inserted the line:

   printf( "I got: %x\n", ch );

right after your getchar() and lo and behold, it told me that
the following character was 0x0A, or linefeed.  That is, the
scanf took the number and the carriage return, leaving the
linefeed in the input stream.  The next getchar got the linefeed
(remember, in UNIX the end-of-line is CR-LF), which is why
your program logic failed.