First key pressed (C++ Console)
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..
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();}
Thanks.
I'm going to just increment a score value rather than using time, as it makes it more comparable.
I'm going to just increment a score value rather than using time, as it makes it more comparable.
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 ...
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 ...
Errmm.. This isn't working at all.
Here's what I have :
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]
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]
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.
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 :
It doesn't actually say what kbhit does.
EDIT: Woops, didn't read that right. You're right, I should just change it to
However, the kbhit problem still stands.
EDIT #2: Nope, It's definately a problem with getch(). Even if I change it to
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]
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]
getch doesn't return until a key is hit!
Solution: Use _kbhit()
What you probably want is something like this:
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";}
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!
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!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement