Link to home
Start Free TrialLog in
Avatar of dave140
dave140

asked on

getch typeahead problem

Whenever I use the getch() or getche() functions and follow with scanf or gets(), the character
typed by the user at the getch()/ getche() prompt is 'echoed' at the gets()/scanf prompt.  The only way to
avoid having the character picked up by these functions is to manually back over it.  It happens
with both getch and getche so I know its not the echo feature of the getche function.  I think it has
something to do with the fact that scanf/gets take arguments.  Any help appreciated.  Thanks.
Avatar of mallrat
mallrat

Ok, haven't done much C coding in a while but I have a couple of Ideas.
I can remember this problem specifically with extended codes.

(I'M sure you know this but anyway)
Most keys on the keyboard will return 1 character.
However, there are many that will return 2 characters. "Extended Codes"
If i remember correctly, all the function keys and Alt? plus a key return extended codes.
Meaning simply that they put two characters in the input buffer.

The getch and getche functions just take the first character from the input buffer and leave the last one there. So if you use gets or Scanf or anything similar. That second character shows up on the console.

If this is your problem you solve it by doing the following.
The first character of an extended code is always the interger 0;
So if you use an interger variable to get the return of getch, you simply test if its equal to zero.
If it is then you know there is another character in the input buffer and you must get it out.
eg.
main()
{
       int ch;

       ch = getch();
       if(ch==0)
       {
           //Program will not pause here. It will just get the next character automatically            since it is an extended code;  
           ch=getch();
       }
       Or it could be that your just not flushing the input buffer enough(this would usually        cause weirder stuff.)
       Hope that was anywhere close to what you need.
dave140, mallrat's answer, although technically correct, is not what you need.

Try the following:

        #include <stdio.h>
        #include <conio.h>
        void main()
        {
            int ch;
            char buff[256];
            ch = getch();
            fflush(stdin)
            gets(buff);
        }

Actually, to be more exact you should do this
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
main()
{
     int ch;
     char buff[256];
     //To get the character 'a'
     again: ch = getch();
     if( ch == 0)
     {
         ch = getch();
         fflush(stdin);    
         goto again;    
     }
     if(ch=='a')
     {
          fflush(stdin);
          gets(buff);
     }
     return 0;
}

 However, you should scrap the use of all the basic functios in the c headers that put input into a string because they can all be crashed rather convincingly.
You should wrute your own algorythm that gets your string input one character at a time.
 It is a good idea because it will get rid of your problem and most other related ones.
 Its not hard to build either but if you have problems just ask for an example here.
 Some of these people could whip it off in a matter of minutes.
By the way. What compiler are you using?
 
Avatar of dave140

ASKER

Mallrat/Alexo, thanks for the responses, but they don't seem to solve my problem.  First of all, I failed to mention that I am compiling these programs in Microsoft VC++ 5.0 as C programs on an Intel Pentium 200mhz MMX machine.  I typed, compiled, and ran your programs and the type-ahead problem was still there.  The buffer never seemed to have anything more in it than what the user enters at the getche 'prompt.'(I examined the contents of the variable used to hold what getch returned)  Mallrat, I like your idea about creating my own algorithm that inputs a string into an array one character at a time, but I believe I would continue have the same problem because there would still be a scanf or gets function following a getch or getche function.  I like the interactive nature of the getche function but it's starting to look as if it can cause more harm than good.
Avatar of dave140

ASKER

Mallrat/Alexo, thanks for the responses, but they don't seem to solve my problem.  First of all, I failed to mention that I am compiling these programs in Microsoft VC++ 5.0 as C programs on an Intel Pentium 200mhz MMX machine.  I typed, compiled, and ran your programs and the type-ahead problem was still there.  The buffer never seemed to have anything more in it than what the user enters at the getche 'prompt.'(I examined the contents of the variable used to hold what getch returned)  Mallrat, I like your idea about creating my own algorithm that inputs a string into an array one character at a time, but I believe I would continue have the same problem because there would still be a scanf or gets function following a getch or getche function.  I like the interactive nature of the getche function but it's starting to look as if it can cause more harm than good.
Avatar of dave140

ASKER

Mallrat/Alexo, thanks for the responses, but they don't seem to solve my problem.  First of all, I failed to mention that I am compiling these programs in Microsoft VC++ 5.0 as C programs on an Intel Pentium 200mhz MMX machine.  I typed, compiled, and ran your programs and the type-ahead problem was still there.  The buffer never seemed to have anything more in it than what the user enters at the getche 'prompt.'(I examined the contents of the variable used to hold what getch returned)  Mallrat, I like your idea about creating my own algorithm that inputs a string into an array one character at a time, but I believe I would continue have the same problem because there would still be a scanf or gets function following a getch or getche function.  I like the interactive nature of the getche function but it's starting to look as if it can cause more harm than good.
Dave140,
Post up your code where the problem exists, (the whole function)and let these guys have a look. Someone is sure to solve it for you my friend
Dave140, What compiler?  What OS?
I can offer you 2 things. First post your code and I will see what is wrong with it. Second if you are interested in generic input functions which work for DOS, Windows and Unix send an email to rbr@physik.kfunigraz.ac.at
rbr, locking a question without providing an answer is not considered professional behavior.  The EE staff frown on it too.
Avatar of dave140

ASKER

Processor:  200 MHZ Intel Pentium MMX
OS: Windows 95
Compiler:  Microsoft Visual C++ 5.0

I am compiling this code as a C program:

#include "stdio.h"
#include "string.h"
#include "conio.h"

void main()
{
      char input, input2[20];

      input = getche();
      printf("\n");
      gets(input2);
}

If I run the program and type 'A' at the getche() function, 'A' appears on the new line before I type anything in response to the gets() function. Any insight appreciated.  

Thanks,

Dave140
Huh?

I run your program (MSVC 4.2, NT)

First, I type 'a' and it gets echoed (getche() echoes the character, getch() does not).
Then, the cursor moves to a new line.
Then I type 'qwerty' which is also echoed.

Seems OK to me.  What's the problem?
try this source:
================

#include "stdio.h"
#include "string.h"
#include "conio.h"

void main()
{
char input, input2[20];

input = getche();
fflush(stdout); // not stdin !!!
printf("\n");
gets(input2);
}

Avatar of dave140

ASKER

Vladimir, Mallrat, Alexo, RBR,

Still, no luck.  Do you think the problem is with something besides the code? I know there are other functions out there that can serve similar purposes, but why doesn't this one work as expected?

Windows 95, MSVC++ 5.0, 200MHZ Pentium MMX.
Avatar of dave140

ASKER

Vladimir, Mallrat, Alexo, RBR,

Still, no luck.  Do you think the problem is with something besides the code? I know there are other functions out there that can serve similar purposes, but why doesn't this one work as expected?

Windows 95, MSVC++ 5.0, 200MHZ Pentium MMX.
I can offer you again, that I'll send you an generic input function, which is tested with many compilers and OSs. Send an email to rbr@physik.kfunigraz.ac.at
Hi Dave140 !
It is very strange. I use Win95, MSVC++ 5.0, 200MHZ Pentium MMX
and the program works perfectly (even without fflush()).
I think that the problem is in another place. Do you check
you source within a large program or separately ?

ASKER CERTIFIED SOLUTION
Avatar of kellyjj
kellyjj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial