Thread: [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 09:39:09
|
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",
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?
---------------------------------------------------------------------
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 );
I assume it didn't work because scanf left the '\n' in the buffer or
something like that...
Any help is greatly appreciated
-- Jon
---------------------------------
|\ |
| \|ightshade -|---
email: nsh...@cr...
website: http://www.crosswinds.net/~nshadez17/
________________________________________________________
1stUp.com - Free the Web
Get your free Internet access at http://www.1stUp.com
|
|
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 |
|
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
|
|
From: lstar36 <ls...@fl...> - 2000-11-13 20:12:03
|
Problem1
The getchar() and getc() functions are both macro's and leave a char (i
think line feed char = 10) in the buffer. getch() and getche() are both DOS
only functions but do not leave a char in the buffer. Unless you are worried
about portability I would use one of them.Otherwise use the scanf() function
to read in a line of code .
scanf("%d", &choice); It will not echo so if you want to see your input you
must writ the char back to the stdio.
Problem2
For reading from the stdin the best way is to use a loop:
char ch;
char string[82];
int i=0;
do
{
ch =getch();
string[i++] = ch;
}while(ch != '\n" || i <= 80)
hope this helps.
---- Original Message -----
From: "Ioannis Vranos" <no...@ya...>
To: <dev...@li...>
Sent: Monday, November 13, 2000 9:14 AM
Subject: RE: [Dev-C++] character buffering with getch() and gets()
>
> > -----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
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/mailman/listinfo/dev-cpp-users
>
|
|
From: Jon K. <rc...@cr...> - 2000-11-13 23:29:26
|
> Problem1
> The getchar() and getc() functions are both macro's and leave a char (i
> think line feed char = 10) in the buffer. getch() and getche() are both
DOS
> only functions but do not leave a char in the buffer. Unless you are
worried
> about portability I would use one of them.Otherwise use the scanf()
function
> to read in a line of code .
> scanf("%d", &choice); It will not echo so if you want to see your input
you
> must writ the char back to the stdio.
As defined in Dev-Cpp's conio.h file, getch() is a macro for getchar(),
not the other way around. Both getchar() and gets() have actual prototypes
in stdio.h. Scanf() does indeed echo to the screen. Finally,
getch()/getche() were the functions that were giving me problems to begin
with
> Problem2
> For reading from the stdin the best way is to use a loop:
> char ch;
> char string[82];
> int i=0;
> do
> {
> ch =getch();
> string[i++] = ch;
> }while(ch != '\n" || i <= 80)
Er... I was hoping to avoid doing my own single-line editor... I'd
like the program to have the ability to backspace/edit and the like, not
just suck in characters.
> hope this helps.
Thanks for your suggestions tho
-- Jon
> ---- Original Message -----
> From: "Ioannis Vranos" <no...@ya...>
> To: <dev...@li...>
> Sent: Monday, November 13, 2000 9:14 AM
> Subject: RE: [Dev-C++] character buffering with getch() and gets()
>
>
> >
> > > -----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
________________________________________________________
1stUp.com - Free the Web
Get your free Internet access at http://www.1stUp.com
|