Jump to content
  • Advertisement
Sign in to follow this  
Keybot

Press any key to continue...

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

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.

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!