Re: [Dev-C++] character buffering with getch() and gets()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jon K. <rc...@cr...> - 2000-11-13 20:27:00
|
> > 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.
Well, that works, but getchar() is line-buffered. Any ideas how I could
do it without hitting <return> after my selection? Not a very big point,
but it makes things a tad more user-friendly.
> > ---------------------------------------------------------------------
> >
> > 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
Yikes, do I need to get better docs for the C standard libraries.
Thanks for all your help,
-- Jon
________________________________________________________
1stUp.com - Free the Web
Get your free Internet access at http://www.1stUp.com
|