Press any key to continue

Started by
15 comments, last by misterags 22 years, 3 months ago
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™
Advertisement
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}  
humanity will always be slaved by its ignorance
Yeah! it works. it had a few errors in it, but nothing any idiot couooldnt figure out.

thanks and later

-Misterags™
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
Gavin Coates
[size="1"]IT Engineer / Web Developer / Aviation Consultant
[size="1"][ Taxiway Alpha ] [ Personal Home Page ]
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
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);}  
#include <stdlib.h>

...

system("PAUSE");

Later,
ZE.


email me.zealouselixir software. msdn. n00biez.
miscellaneoulinks

[twitter]warrenm[/twitter]

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
#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().
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

This topic is closed to new replies.

Advertisement