Archived

This topic is now archived and is closed to further replies.

graveyard filla

HOW do i press any key to continue??????

Recommended Posts

high, i have a simple text RPG game and when the player kills someone, i want it to say "press any key to continue" and then execute then next line of code ive actually come up with this problem a lot and cant seem to fix it. the best way i came up with to do this is char holder; cout>>"Press any key then press enter to continue">>endl; cin << holder; but this forces the user to press a key THEN enter to continue the game. i also thought about using getline, considering it has a terminating argument IE when you press that key it will end therfore doing string holder; cout>>"Press enter twice to continue">>endl; getline (cin,holder,'/n') but that caused a whole load of problems throughout my whole game (i think it has something to do with cin.ignore() but i just would rather not mess with getline at all. anyway, so how do i do it? the problem is i have a bunch of clearscreens inside my game, so some stuff gets wiped out before you can see it, so my idea was to add a "press any key to continue" before i cleared the screen, so you could see what you were suposed to see. now that i think about it, isnt there a sleep() function that will wait an x amount of seconds before it goes to the next line of code? what is the sytax for that, and what headers to i include? maybe ill just make it delay instead, it seems to make more sence, although id still like an answer to my first question because i come up with that problem a lot. thanks for any help.... [edited by - graveyard filla on January 25, 2004 3:49:41 PM] [edited by - graveyard filla on January 25, 2004 3:50:33 PM]

Share this post


Link to post
Share on other sites
Stephen R    721
use getch();

It reads in a single character from the command line and doesn''t display it.

Use getche() if you want the character to be displayed.

Share this post


Link to post
Share on other sites
Greatwolf    125
And your other question about delaying, you can use Sleep(ms); let say if you want to delay 5 seconds you would use Sleep(5000); etc.

You might need to include windows.h for this btw.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
kVandaele    138
isn''t there a system call as well? One the even displays the "Press any key to continue..." line?

system("PAUSE");

don''t remember which include it needs but it''s bound to be one of these:
#include <iostream>
#include <conio.h>
#include <cstdlib>

Share this post


Link to post
Share on other sites
Fidelio66    164
quote:
Original post by kVandaele
isn''t there a system call as well? One the even displays the "Press any key to continue..." line?

system("PAUSE");



That''s really really bad. What is does is start a second process, command or cmd, and execute one batch command, pause, then the command processor exits again. Because it has to be able to execute old dos programs it allocates 640K plus the whole dos interface.
System() is for the very lazy, a decent programmer should solve it the correct way.

Share this post


Link to post
Share on other sites
i have a nother question, what do i do if i want to say...

right in the begining of the game i gave a choice, of either showing options or starting the game. i would write

cout << "Press any key to play or press O for options" << endl;

now what? if i do a
cin >> play;

if (play == O)
{go options;}

BUT, this requires the player to press any key THEN press enter if they DONT want to see the options. how do i allow it so they press any key(without pressing enter) and it goes to the next line of code, OR IF they press O it will go to options.

heeelp ?

also some example source would be greatly appreciated!!!

Share this post


Link to post
Share on other sites
Odoacer    140
I don''t think it''s possible with the console. You could try:

char holder;
cout>>"Press enter to continue or O for options">>endl;
cin.get(holder);
if(holder == ''o'')
[...]

That way you''d only have to press enter to go, ''o'' otherwise.
Hope that helps.

Share this post


Link to post
Share on other sites
Nervo    344
this tip may not work across all implementations, but try this out:

#include <conio.h>
getche();

''getche'' takes unbuffered input and returns the character pressed.

Share this post


Link to post
Share on other sites
SarsDP    100
I don''t think it''s possible with the console. You could try:

char holder;
cout>>"Press enter to continue or O for options">>endl;
cin.get(holder);
if(holder == ''o'')
[...]

That way you''d only have to press enter to go, ''o'' otherwise.
Hope that helps.


you could do


char holder;
cout>>"Press enter to continue or O for options">>endl;
getch(holder);
if(holder == ''o'' || holder == ''O'')
{
//options code
}
// if they hit something other than o then it will just continue

Share this post


Link to post
Share on other sites
Odoacer    140
quote:
Original post by Nervo
this tip may not work across all implementations, but try this out:

#include <conio.h>
getche();

''getche'' takes unbuffered input and returns the character pressed.



He''s got it spot on - I didn''t know about the getche function.
char c1 = getche();
if(c1 == ''o'' || c1 == ''O'')
[..]

As Nervo said though, it''s not standard, but it seems like a good solution to me.

Share this post


Link to post
Share on other sites
Doc    586
Just a note that none of the solutions to this problem are part of the standard C or C++ langauges. Internal buffering by newlines is standard and works anywhere - you could probably get away with "Hit enter to continue" if you wanted/needed to keep to standard, but you probably don''t need to. Waiting for a keypress assumes there is realtime user feedback, and as I heard someone else say once "C doesn''t assume you even have a keyboard".

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

Share this post


Link to post
Share on other sites
Odoacer - thanks, your code worked, not exactly how i hoped it worked (the user has to press enter, otherwise they have to press a key THEN enter) but good fuckin enough. thanks man.

SarsDP - your code didnt work. it said getch doesnt receive 1 parameters. thanks anyway

Share this post


Link to post
Share on other sites
kVandaele    138
this is what i did (don''t know about "standard c or c++", but it looks like it):

// show help or not?/

while(input!=''h''&&input!=''H''&&input!=''s''&&input!=''S''&&input!=''\n''&&input!=''\r''){
std::cout<<"Press ''h'' for help, ''s'' or [Enter] to start the game..."<<std::endl;
input=getch();
}
if(input==''h''||input==''H''){
inihelp();
}

probably not the best way to check for upper/lowercase, and i don''t think you need the ''\n'' but it shouldn''t hurt

Share this post


Link to post
Share on other sites
Ademan555    361

#include <iostream>
#include <conio.h>

int main()
{
std::cout<<"Press O for options, hit enter to continue...\n";
switch(getch()
{
case ''O'':
case ''o'':
//OPTIONS MENU

break;
case ''\n'':
//CONTINUE

break;
}
return 0;
}

Share this post


Link to post
Share on other sites