943,597 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2653
  • C++ RSS
Dec 1st, 2006
0

need a function that woks a little bit like getch

Expand Post »
Hello! here is what I wanna do:
I have a while() statement, and inside of it I have some instructions to repeat. My problem is that, I want somehow to stop that loop ONLY when I press a key... I it's kinda like getch() but getch() waits for a keypress.... Any help is appreciated Thanx in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uu666 is offline Offline
13 posts
since Aug 2006
Dec 1st, 2006
0

Re: need a function that woks a little bit like getch

Create a thread which will have only getchar. If something pressed then set a bool value in that thread. Of course in while loop will break when the bool value is set.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Dec 2nd, 2006
0

Re: need a function that woks a little bit like getch

Click to Expand / Collapse  Quote originally posted by uu666 ...
Hello! here is what I wanna do:
I have a while() statement, and inside of it I have some instructions to repeat. My problem is that, I want somehow to stop that loop ONLY when I press a key... I it's kinda like getch() but getch() waits for a keypress.... Any help is appreciated Thanx in advance.
I assume you have getch() available. Not all compilers do, making any code using it non-portable. But if you do, look to see if the compiler has a function called kbhit(). That's the function you're looking for.
Last edited by WaltP; Dec 2nd, 2006 at 2:01 am.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,717 posts
since May 2006
Dec 2nd, 2006
0

Re: need a function that woks a little bit like getch

> somehow to stop that loop ONLY when I press a key
You need to say which OS and compiler you're using.
There is no portable answer to this question, so we need specifics.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 2nd, 2006
0

Re: need a function that woks a little bit like getch

i tried with kbhit() but isnt actually what i was looking for... im using borland c++ 3.1.

i have a for loop and i want that when the "h" key is pressed to do something.... however i dont wanna cease program's execution to wait for that key like getch() does.......
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uu666 is offline Offline
13 posts
since Aug 2006
Dec 2nd, 2006
1

Re: need a function that woks a little bit like getch

> i tried with kbhit() but isnt actually what i was looking for
Did you try
C++ Syntax (Toggle Plain Text)
  1. if ( kbhit() ) {
  2. ch = getch();
  3. }
kbhit() doesn't do any reading, it only tells you if there's something there.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 2nd, 2006
0

Re: need a function that woks a little bit like getch

Thanx alot dude I owe you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
uu666 is offline Offline
13 posts
since Aug 2006
Jan 22nd, 2007
0

Re: need a function that woks a little bit like getch

Click to Expand / Collapse  Quote originally posted by Salem ...
> i tried with kbhit() but isnt actually what i was looking for
Did you try
C++ Syntax (Toggle Plain Text)
  1. if ( kbhit() ) {
  2. ch = getch();
  3. }
kbhit() doesn't do any reading, it only tells you if there's something there.
In your above code the kbhit will pick up on the first keystroke. but
C++ Syntax (Toggle Plain Text)
  1. ch = getch();
will wait for another stroke, no?

I want to loop until a key is pressed. Then I want the ability to check that key that was pressed without havin to press it again
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
amishosh is offline Offline
59 posts
since Oct 2006
Jan 22nd, 2007
0

Re: need a function that woks a little bit like getch

It's been a while since I've used kbhit, so don't shoot me i I'm wrong ; but if I remember correct kbhit will only tell you that a key is pressed. By saying ch = getch() you will actually get the key that is pressed, so if you want to loop until a char is pressed just use something like:

  1.  
  2. while (!(kbhit()));
  3. ch = getchar();
Last edited by Nick Evan; Mar 26th, 2010 at 10:40 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jan 22nd, 2007
0

Re: need a function that woks a little bit like getch

> In your above code the kbhit will pick up on the first keystroke.
Read it again!.

kbhit() does NOT read a key, it merely tells you whether there is a key to be read.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with Creating a Command Line Menu
Next Thread in C++ Forum Timeline: reading numbers from a text file and putting them into an integer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC