Jump to content
  • Advertisement
Sign in to follow this  
fatnickc

First key pressed (C++ Console)

This topic is 4883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Having not done much (any) C++ console programming for a while, I thought it would be nice to make a simple game to get used to it again - something more fun than 3D or whatever. And that's where I still am now - well, about 10 lines of code down. I coudldn't think of how to get a keypress from the user without them pressing enter after it, using cout. So I used getch(), and let the user just press enter to make getch() happy and let the program carry on. Now, I face my problem. How can I stop a loop (incrementing x until enter is pressed) to stop when enter is pressed? No idea myself. I've tried whatever I've thought of, but nothing works. Hmmmmm..

Share this post


Link to post
Share on other sites
Advertisement
Should work. '\n' is enter, IIRC, otherwise try '\r'. The break keyword is what you're after - breaks out of the inner-most loop (for, while, do) or case statement.

while(timer > 0)
{
if(getch() == '\n')
break;
timer.tick();
}

Share this post


Link to post
Share on other sites
Thanks.
I'm going to just increment a score value rather than using time, as it makes it more comparable.

Share this post


Link to post
Share on other sites
for a console game,
getch() is your friend ... but not as much as kbhit() ...

kbhit() tells you WHEN a key is pressed ... if a key is pressed, THEN you call getch() ...

this way, your game never waits for no good reason ...

Share this post


Link to post
Share on other sites
Errmm.. This isn't working at all.
Here's what I have :

while(times>0)
{
if(getch()=='\n')
break;
times++;
}


It compiles, but times never gets greater than 0.

And crucifier, where is kbhit() written out? Can't find it in any of the headers I could possibly include.

[Edited by - fatnickc on June 1, 2005 12:29:58 PM]

Share this post


Link to post
Share on other sites
well isn't time initailized to 0? If so you break the loop instantly and never enter it since its already equal to 0 and not greater than.

Share this post


Link to post
Share on other sites
Yes, but the loop ISN'T being broken immediately, is it? The break; is inside an if statement, so it will only be broken if I press return/enter. (see EDIT)

And kbhit() is in conio.h, but only like this :
 int	kbhit (void);

It doesn't actually say what kbhit does.

EDIT: Woops, didn't read that right. You're right, I should just change it to
if(times>=0)
{
...
}


However, the kbhit problem still stands.

EDIT #2: Nope, It's definately a problem with getch(). Even if I change it to
while(1==1)
{
if(getch()=='\n')
break;

cout << "going";
}

going is never being printed to the screen. If, however, I put going above the if statement, going is printed once only.


[Edited by - fatnickc on June 2, 2005 4:24:22 AM]

Share this post


Link to post
Share on other sites
getch doesn't return until a key is hit!

while(1==1)
{
if(getch()=='\n') // getch is called, then the program waits until a key is hit, when a key is hit getch returns the ascii code for the key.
break;

cout << "going";
}





Solution: Use _kbhit()
What you probably want is something like this:

while(1==1)
{
if (_kbhit() != 0){ // Returns immediatly, if a key has been pressed since the last call to getch, it returns non-zero
cout << "Key pressed! Reading key";
if(getch()=='\n') // getch is called, since kbhit told us that there is a key pressed it will return immediatly with the ascii code for the pressed key.
break;
}
cout << "going";
}


Share this post


Link to post
Share on other sites
The nearest thing to _kbhit() in my conio.h is int _conio_kbhit(); .
However, I can't use that because that is the only thing said about it. There is nothing where the actual function is written out.
In conio_mingw.h, however, _conio.h is, everything aprt from the actual function being written!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!