145

What would be the best way to wait for user input in console application?

std::cout << "press any key to exit...";
// wait for user to hit enter or another key
Ivars
  • 2,375
  • 7
  • 22
  • 31
  • 3
    There is no standard way to respond to any key press (as console input function waits for the user to hit "enter") just as there is no standard way to respond to mouse operations or any other input device. – CashCow Jan 21 '14 at 12:12
  • 8
    If you want to pause until pressing enter: `std::cin.ignore();` – Stevoisiak Aug 02 '18 at 17:01

4 Answers4

269

Several ways to do so, here are some possible one-line approaches:

  1. Use getch() (need #include <conio.h>).

  2. Use getchar() (expected for Enter, need #include <iostream>).

  3. Use cin.get() (expected for Enter, need #include <iostream>).

  4. Use system("pause") (need #include <iostream>, Windows only).

    PS: This method will also print Press any key to continue . . . on the screen. (seems perfect choice for you :))


Edit: As discussed here, There is no completely portable solution for this. Question 19.1 of the comp.lang.c FAQ covers this in some depth, with solutions for Windows, Unix-like systems, and even MS-DOS and VMS.

Chnossos
  • 9,971
  • 4
  • 28
  • 40
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • 4
    In Microsoft VS2012, Use include #include and _getch(). – CreativeMind Jan 21 '14 at 12:14
  • @herohuyongtao: `getchar` would expect _Enter_. [See this](http://stackoverflow.com/q/1798511/183120). – legends2k Jan 21 '14 at 12:18
  • 1
    @herohuyongtao: It gives this error "error C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch." – CreativeMind Jan 21 '14 at 12:28
  • @RonakPatel I don't use Visual Studio. Would those same includes apply for G++ as well? – RPiAwesomeness Nov 25 '14 at 02:06
  • 4
    I'd strongly suggest avoiding the use of getch(), because it only works with a physical console, not stdin, and as such won't work with pipe or file redirection. – Roger Sanders Jul 01 '15 at 04:36
  • system("pause") worked for me, it seems like the simplest solution... I'm on VS Community Edition 2013. – Greg Oct 17 '15 at 00:10
  • 4
    for linux, use pause() from unistd.h. pause command exist only on microsoft platform and few pdp or dos related ones. Some distros have it as alias declared – Swift - Friday Pie Feb 02 '17 at 14:31
  • @Swift-FridayPie Please don't use `pause` from unistd.h. It waits for a signal, not a user input: https://stackoverflow.com/a/15992981/5601591. Please, please at least have the courtesy to use `system("pause")` only when `_WIN32` is defined and wait for a line break everywhere else. I hope that's not asking too much. – Jack G Oct 20 '21 at 20:27
  • @JackG necrocomment, but that's fine, I never wrote it wait fr user input, yeah, though that quite established way to wait (or even wrap-up process, e.g. how old versions ffmpeg did it). User have to send signal by Ctrl-C or its analog in this case, which they usually can. How "less"\"more" do work? That's where it should be looked up. Neither only waiting for line break is a good way, on some systems you may end with frozen terminal instance if user has been disconnected from host. Wait for nd-of-transmission but also setup signal handlers, perhaps? – Swift - Friday Pie Oct 21 '21 at 06:03
  • conio.h is windows only. https://en.wikipedia.org/wiki/Conio.h – Amin Ya Jun 23 '22 at 07:57
19

a do while loop would be a nice way to wait for the user input. Like this:

int main() 
{

 do 
 {
   cout << '\n' << "Press a key to continue...";
 } while (cin.get() != '\n');

 return 0;
}

You can also use the function system('PAUSE') but I think this is a bit slower and platform dependent

Volomike
  • 23,743
  • 21
  • 113
  • 209
CMS
  • 704
  • 3
  • 9
  • 32
  • 5
    system('PAUSE') is not available on all systems... – Tiago Mar 25 '15 at 15:50
  • Why does this prompt twice for the first time it is used? – Marcello B. Dec 02 '20 at 21:18
  • 1
    That will loop indefinitely if `std::cin` is closed (as `get()` will return `EOF`). – Toby Speight Sep 29 '21 at 11:18
  • @MarcelloB. Maybe you typed one character before pressing enter(`\n`). That while loop works until `cin.get()` gets the `'\n'` in the above code. E.g. if you type 3 characters and press enter, then it will prompt "Press a key to continue..." 3 times. – starriet Jul 20 '22 at 01:56
10

There is no "standard" library function to do this. The standard (perhaps surprisingly) does not actually recognise the concept of a "keyboard", albeit it does have a standard for "console input".

There are various ways to achieve it on different operating systems (see herohuyongtao's solution) but it is not portable across all platforms that support keyboard input.

Remember that C++ (and C) are devised to be languages that can run on embedded systems that do not have keyboards. (Having said that, an embedded system might not have various other devices that the standard library supports).

This matter has been debated for a long time.

CashCow
  • 30,981
  • 5
  • 61
  • 92
8

You can try

#include <iostream>
#include <conio.h>

int main() {

    //some codes

    getch();
    return 0;
}
Edward Falk
  • 9,991
  • 11
  • 77
  • 112
JustCode
  • 115
  • 5