Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!xanth!mcnc!rti!sunpix!matthew From: matthew@sunpix.UUCP ( Sun NCAA) Newsgroups: comp.lang.c Subject: Re: learning c Keywords: getchar() Message-ID: <584@greens.UUCP> Date: 3 Apr 89 15:46:10 GMT References: <1091@bnlux0.bnl.gov> Distribution: usa Organization: Sun Microsystems, Research Triangle Park, NC Lines: 52 In article <1091@bnlux0.bnl.gov>, baron@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 say } the is passed to the next thing seeking input and so now } name is just 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@bnlux0.bnl.gov, baron@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 |