Re: [Dev-C++] character buffering with getch() and gets()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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
>
|