help with kbhit() in MSVC++

This is a discussion on help with kbhit() in MSVC++ within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <conio.h> #include <stdio.h> int main() { while(1) { if (kbhit()) { cout << "The keyboard has ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    help with kbhit() in MSVC++

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    
    int main()
    {
    	while(1)
    	{
    		if (kbhit())
    		{		
    			cout << "The keyboard has been hit" <<endl;
    			fflush(stdin);
    		}
    	}
    	return 0;
    }
    what im trying to do is have the message display only when the keyboard is hit. the problem im having is that once a button is hit, it continuously loops the message. how can i just key the message to display only when a key is hit?

    also, as u can see, i tried flushing the stdin, but that didnt work.
    thanks !

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    while(1)
    {
    if (kbhit())
    {
    cout << "The keyboard has been hit" <<endl;
    fflush(stdin);
    break;
    }
    }
    system("pause");
    return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    why does it exactly do that th0?
    i mean, loop the displaying of the message?

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    it works once, then exits the loop. i need to constantly be lookin for the keyboard hit

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Code:
    if (kbhit())
    {
    	cout << "The keyboard has been hit" <<endl;
    	_getch();
    }

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    it would continue to run if you change it around a bit. The break tells it to leave the if statement. Otherwise it was always going to display a key was hit, because it was.

    The break works the same way as it would in a switch statement. The pause is in there so it wont just close on you. Theres several ways your problem could be addressed, i just chose this way as mine. Search kbhit() im sure there has been questions before.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    thanks for all ur help everyone... the _getch(); worked!

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by RoD
    The break tells it to leave the if statement
    actually a break will exit a loop or a switch, not an 'if' statement. having a break inside an 'if' will exit the loop the 'if' is in.

  9. #9
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Didnt know that, seems i was mis-informed(ing), thnx for the correction.

  10. #10
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    That was similar to a practice question on my AP test!

    The if break thing...
    If you ever need a hug, just ask.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with MSVC, Mingw32 and Code::Blocks
    By Brafil in forum C Programming
    Replies: 11
    Last Post: 10-12-2009, 12:34 PM
  2. MSVC behaves strange
    By ripper079 in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2003, 08:34 PM
  3. Blender3D, XML, and MSVC ???
    By Grumpy_Old_Man in forum Game Programming
    Replies: 0
    Last Post: 08-24-2003, 08:00 PM
  4. GCC (cygwin) much faster than MSVC, Borland?
    By Sargnagel in forum C Programming
    Replies: 15
    Last Post: 08-05-2003, 04:15 AM
  5. Pausing for input, with kbhit() still detecting keys
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 06:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21