Archived

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

Press any key to continue

This topic is 5816 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

Hey ya''ll. I know there''s a lot of ppl w/ skill here, so could someone plz tell me how to write some "press any key to continue" code so i don''t have to say "press any key and enter to continue". This is in plain ol'' win32 console with msvc++. and try to keep it as simple as possible... thanks guys. -Misterags™

Share this post


Link to post
Share on other sites
there is no compiler here so if it have an error please somebody with one fix it

  
#include <conio.h>

#include <iostream>

using namespace std;

int main (void)
{
cout << "Press any key to continue:" << flush; // message

while (!kbhit()) {} // while keyboard is not hit

char garbage = getCh () // not sure check conio.h to see the sintax

// .... rest of your code

}

Share this post


Link to post
Share on other sites
or simply:

#include conio.h
int main()
{
cout << "press any key to continue";
getch();
}

you dont even need to assign the return value from getch, as you do not need to check it.

EDIT: forgot the header

Edited by - gavco98 on January 10, 2002 2:47:13 PM

Share this post


Link to post
Share on other sites
just use a function or macro.
      
inline void PressReturnKey(){cout << "\nPress Return To Continue"; cin.get();}


if u use namespace std, you really should try not to use conio.h (yes, i realize i dont always practice what i preach...)

Edited by - evilcrap on January 10, 2002 4:06:36 PM

Edited by - evilcrap on January 10, 2002 4:18:46 PM

Share this post


Link to post
Share on other sites
C:
and you shouldnt use kbhit() to wait for input. use getch(), there is no reason to make a wait loop visible like that:
  
while(!kbhit())
; //ugly

getch(); //better



the only reason i can of to ever use kbhit() is to test for specific key input
  
inline bool TestKey(char c)
{
char k = 0;
if (kbhit())
if( !(k = getch()) ) k = getch();
return (k == c);
}

Share this post


Link to post
Share on other sites
From a usability standpoint the "press any key to continue" phrase should be avoided. Write "press space to continue" or some other key instead (maybe "press enter to continue" if you''re having trouble coding for other keys). Not only does "any key" confuse stupid users but if you use it, you also have to make sure that ANY key really works. There are numerous examples of programs telling the user to press any key but where shift, ctrl, alt and/or the function keys don''t work.

Henry

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
#include <iostream.h>
#include <conio.h>

void main()
{
cout << "Press any key to continue . . .";
cout.flush();
getch();
}

That''s the way I do it, but if you use cout <<, make sure you use cout.flush() before getch().

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
...if you use cout <<, make sure you use cout.flush() before getch().



...If you don't, then, if you have the following:

    
cout << "A\n";
cout << "B\n";
cout << "Press any key...\n";
getch();
cout << "C\n";


...your output may look like this:


A
___WAITS FOR KEYPRESS___
B
Press any key...
C


The iostream functions add data to a buffer which is regularly flushed to/from the console. The conio functions immediately perform actions. For this reason, they are not synchronized, and eevents may occur out of order. This is why you need to tdo the cout.flush().

Edited by - TerranFury on January 10, 2002 6:07:40 PM

Share this post


Link to post
Share on other sites
cout.flush() only needs to be used if you use iostream.h
if you use iostream, and namespace std, you dont need to flush.
  
#include <iostream>
using namespace std;
void main()
{
for(int i =0; i< 100; i++)
cout << i + ''a'';
cout << "\npress any key";
getch();
}//main



if you dont want to use std, then prior to output, you can specify an ios flag, unitbuf, to flush frequently.
  
cout.flags(ios::unitbuf);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well "I" personally do not like to use Namespace Std because it bloats my program size too much.
Why is that BTW?

Share this post


Link to post
Share on other sites
quote:
Original post by EvilCrap
cout.flush() only needs to be used if you use iostream.h
if you use iostream, and namespace std, you dont need to flush.

Sorry, no cigar.

quote:
Anonymous Poster
Well "I" personally do not like to use Namespace Std because it bloats my program size too much.
Why is that BTW?

Beccause you''re not supposed to import the entire namespace. The objective of namespaces is to enable you resolve clashes as well as minimize entities brought into your translation unit. Doing
using namespace std; 
defeats the purpose and is just plain bad programming practice.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites