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.

5
  • It is operating system specific. On which OS are you coding? Posix has sigsuspend Commented Dec 31, 2013 at 15:02
  • 1
    Are you looking to pause a single application, or the whole operating system? Commented Dec 31, 2013 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 :) Commented Dec 31, 2013 at 15:04
  • @atk C++ console programming. Commented Dec 31, 2013 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? Commented Dec 31, 2013 at 17:24

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

it's so C-ish, cin will also do the trick and it's more C++-ish
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)?
@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.
@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 to check for input on STDIN_FILENO and then read that input.
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!

1 Comment

True, but maybe it’s not at the end? “Please hit any key after you’ve made sure the bathroom is empty and the door is closed” …
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.