RE: [Dev-C++] character buffering with getch() and gets()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <no...@ya...> - 2000-11-13 15:13:28
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...]On Behalf Of Jon Keim > Sent: Monday, November 13, 2000 11:39 AM > To: Dev...@li... > Subject: [Dev-C++] character buffering with getch() and gets() > > > problem 1: > I'm developing a piddly little program from a learning book, and in my > main(), I've got a statement to get the user's choice from a menu > that looks > like this: > > choice = getche(); > > it takes the '1' character required and successfully activates a > particular function. Once inside the function, however, I have this input > statement, to prompt for and get some data for the string "title" in a > struct array. > > printf( "Book title : " ); > gets( catalog[ recordCount ].title ); > > Simple, right? Nope. The prompt comes up > > Book title : 1 > > the gets() reads the 1 from the getche() up in the main function... I > have tried all sorts of odd things, placing getch()'s and getche()'s > after/before statements to clear the buffer, substituted a scanf( "%s", getch(), getche() are not standard C/C++ functions. Better use getchar() or getc(stdin) in the following style: choice=getchar(); getchar(); getchar() is equivalent of getc(stdin) so use whatever one you like. The second getchar() is used to clear '\n' (the enter) from the stdin. > foo ) for the gets(). Even tried testing to see if there was something in > the buffer with kbhit(), and that comes up negative. Any idea why this is > happening? Implement my above suggestion. > --------------------------------------------------------------------- > > problem 2: > any idea how to limit an inputted string to 80 characters and still be > able to read spaces? I tried the following in my above program and it > screwed things up FAR worse... > > scanf( "%80[^\n]", string ); One way is fgets(): fgets(string, 80, stdin); It reads 79 characters, and places '\0' at the end. Ioannis _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com |