learning c

Sun NCAA matthew at sunpix.UUCP
Tue Apr 4 01:46:10 AEST 1989


In article <1091 at bnlux0.bnl.gov>, baron at bnlux0.bnl.gov (ed baron) writes:
} Maybe this isn't the best place to post this but it seemed reasonable.
} 
} I am trying to learn c out of a book that seems to be wrong on many
} things, but anyway i can't seem to figure out how getchar() should
} work. consider the following
} 
} char name [10];
} int c;
} 
} for(;;)
} {
} puts("enter 9 to exit");
} c = getchar();
} if(c == 9)
} break;
} puts("enter name: ");
} gets(name);
} .
} .
} .
} }
} 
} now getchar gets one character but not until it sees the terminator
} (carriage return) but since it only gets one character and the
} carriage return is one character if you enter 1<cr> say
} the <cr> is passed to the next thing seeking input and so now
} name is just <cr> i.e there is no opportunity to respond to the naame
} prompt. then i tried using gets(c) in place of getchar c (andd
} declaring c to be char) but that won't compile. what is the proper
} way to do this? please respond via mail. thanks.
} 
} ed baron (baron at bnlux0.bnl.gov, baron at bnlux0.bitnet)


    I know your problem well.  What you are looking for is a 'getch()' or
'getche()' function in place of the 'getchar()' function.  Barring that try

c = getchar();
fflush(stdin);

    The variable 'c' will get the first character of input, and the remainder
of the line (plus the newline) will get flushed away.




-- 
Matthew Lee Stier                         |
Sun Microsystems ---  RTP, NC  27709-3447 |        "Wisconsin   Escapee"
uucp: { sun, mcnc!rti }!sunpix!matthew    |
phone: (919) 469-8300 fax: (919) 460-8355 |



More information about the Comp.lang.c mailing list