Thread: KBHIT isn't working

  1. #1
    Shadow12345
    Guest

    KBHIT isn't working

    The user is asked whether he/she would like to continue with the program.

    Code:
    cin.ignore(); //ignores the previous input so it cannot interfere with this section of code
    
    if( kbhit() ) {    //if the keyboard is hit
    KeyCode = getch();  //KeyCode(declared as an int) equals getchar
    if(KeyCode != 89)  //if the GetChar is not 'Y' (or 89 in Decimal)
    EnteringData = false; //
    }
    for some reason that code does not seem to work. Even if the user does not hit Y the program continues.

    Oh yeah I almost forgot to mention you can look up the characters' decimal values at www.asciitable.com

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    ignore() is acting on the second input also. This member function is usually used as a means to ignore certain characters on an input. I'm not sure of this, but I can only assume that ignore with no arguments (it usually accepts 2) will ignore every character on the input indefinitely. Hence the behavior on your application

    Do cin.ignore(256,'\n')... that way, ignore will cease to affect cin as soon as the previous input will either reach 256 ignored characters or you hit the return key.

    Or

    Just remove the line altogether. I don't see why any previous cin will affect kbhit()
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  2. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  3. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  4. kbhit()
    By McNab in forum C Programming
    Replies: 4
    Last Post: 04-30-2002, 06:57 PM
  5. need more help on kbhit()
    By lordvlaad() in forum C++ Programming
    Replies: 0
    Last Post: 04-20-2002, 02:07 AM