4

I've been looking for this on the internet for soooooo long. Is there a way you can press any key and it immediately stops the pause and carries on with executing the code but it doesn't show up the key you pressed on the screen (like system("pause"))?

People said cin.get() and stuff like that, however, if I use that, I have to press any key AND it displays on the screen and you have to press enter after that.

SomeRandomGuy
  • 111
  • 1
  • 8
  • It is operating system specific. On which OS are you coding? Posix has [sigsuspend](http://pubs.opengroup.org/onlinepubs/009695299/functions/sigsuspend.html) – Basile Starynkevitch Dec 31 '13 at 15:02
  • 1
    Are you looking to pause a single application, or the whole operating system? – atk Dec 31 '13 at 15:03
  • I have OSX (unfortunately) but I would like to make it compatible with windows too so I would like to know the answer for both systems please :) – SomeRandomGuy Dec 31 '13 at 15:04
  • @atk C++ console programming. – SomeRandomGuy Dec 31 '13 at 15:05
  • So you are writing a c++ console application, and looking for a way to wait for any key to be pressed before continuing execution, but prevent that last input from displaying in the consolw, correct? – atk Dec 31 '13 at 17:24

3 Answers3

4

Since you're referencing system("pause") I guess you're using Windows, then you can use _getch to wait for any key.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • it's so C-ish, `cin` will also do the trick and it's more C++-ish – user2485710 Dec 31 '13 at 15:05
  • Thank you that helps but is there any other way to do it on OSX too (as I would like to make it cross-platform)? – SomeRandomGuy Dec 31 '13 at 15:06
  • 2
    @user2485710 If I use cin I have to type any thing then press enter or just press enter. I want to make it so you can press any key. – SomeRandomGuy Dec 31 '13 at 15:12
  • 2
    @user3149463 Unfortunately there is no platform independent way to do it. On OSX (and Unix-like systems like Linux) you can use e.g. [`select`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/select.html) to check for input on `STDIN_FILENO` and then `read` that input. – Some programmer dude Dec 31 '13 at 15:14
2

Joachim Pileborg has already mentioned _getch as a Windows-specific technical solution.

However, that's a solution looking for a problem … because there's really no problem.

To see the last output from your console program, you can use any of these methods:

  • Run the program from the command line, e.g. an instance of Windows' standard [cmd.exe] command interpreter.

  • Run the program from an IDE, such that it stops at the end. E.g. in Visual Studio just use [Ctrl F5].

  • Run the program in a debugger, with a breakpoint on the closing } of main. E.g. in Visual Studio, add that breakpoint and run via keypress [F5].

Especially when you try the first bullet point, you will notice that having a _getch or system( "pause" ) or such at the end of the program, has no advantage and can be quite annoying!

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

I don't know about Windows (where apparently _getch() is the way to go) but on UNIXes you can set the standard input stream (file descriptor 0) into non-canonical mode using tcgetattr() and tcsetattr() to get the key immediately. To suppress the key presses from showing up, you'll need to also disable echoing:

termios old_tio, new_tio;
int rc = tcgetattr(0,&old_tio);
new_tio=old_tio;
new_tio.c_lflag &=(~ICANON & ~ECHO);
rc = tcsetattr(0,TCSANOW,&new_tio);

std::string value;
if (std::cin >> value) {
    std::cout << "value='" << value << "'\n";
}

rc = tcsetattr(0,TCSANOW,&old_tio);

This code

  1. first gets the current state of the terminal flags
  2. clears the ICANON and ECHO flags
  3. read hidden input (in this case a string but it can be an individual key, too)
  4. restores the original settings

There is, unfortunately, no portable way of dealing with these setting, i.e., you will need to resort to platform specific uses. I think the use of tcgetattr() and tcsetattr() is applicable to POSIX systems, though.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380