Press any key to continue...

Started by
13 comments, last by lizun 18 years, 11 months ago
Hi I would just like to know to implement a 'Press any key to continue...' feature in c++. Now I have googled the subject but nothing that really does what I want came up. I know you can do it using the system("PAUSE") in win32 platform but this is just a cheap OS trick (Ok, maybe not that cheap) and I would like to know how to do this for any platform. Any help would be greatly appreciated.
Advertisement
mmm a simple way is to use

cout<<"Press any key to continue...";
cin>>var;

And thats it!
Remember that a console program is supposed to be run from the console. And then the 'Press any key to continue...' is just irritating.

[Edited by - doho on May 11, 2005 11:55:10 AM]
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385

that link shows you a bunch of ways but this is waht I have

#include <cstdlib>
#include <iostream>


using namespace std;

int main(int argc, char *argv[])
{
cout<<"Press any key..."; getchar();

return EXIT_SUCCESS;
}


It works in dev c++ I tested it
Surprisingly, there isn't really a platform-independent way to do it. The basic problem is that the C/C++ standard libraries don't offer any functions to control the echoing and/or buffering of input. So you can do "press return to continue", but you can't reliably do "press any key to continue" and when you do "press return to continue" what that really means is "type as much stuff as you want, and then press return to continue".
Didn't know about getchar(). Sneftel, since it's (apparently) part of cstdio, shouldn't it be cross-platform? Wait, no. Forget I said that.


jfl.
Yes, getchar() is cross-platform. However, the character will not be transmitted to the program (and picked up by getchar()) until you actually press return. Your C program does not directly read from the keyboard; it reads from a file descriptor that is handled by the "console". And whether the data is fed character-by-character or line-by-line is a console attribute.

On Win32, look at <conio.h> and kbhit(), on POSIX, look at <curses.h> and cbreak()
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Yes, getchar() is cross-platform. However, the character will not be transmitted to the program (and picked up by getchar()) until you actually press return. Your C program does not directly read from the keyboard; it reads from a file descriptor that is handled by the "console". And whether the data is fed character-by-character or line-by-line is a console attribute.

On Win32, look at <conio.h> and kbhit(), on POSIX, look at <curses.h> and cbreak()


Thanks for the explanation. I had realized this shortly after posting, thusly the strikeout and not-so-covert slinking away from the subject.

jfl.
Couldn't you also just do...

cout << "Press a key to continue.";
cin.get(ch);

?? That's how I've done it in the past.
Quote:Original post by Kage no HoNeko Joshi
cin.get(ch);

That has the same problem as getchar(). It'll only work if your terminal does not perform line-buffering.

This topic is closed to new replies.

Advertisement