How to Wait for User Input in C++

  1. Use cin.get() Method to Wait for User Input
  2. Use getchar Function to Wait for User Input
  3. Use getc Function to Wait for User Input
  4. Avoid Using system("pause") to Wait for User Input

This article will introduce C++ methods to wait for user input. Note that the following tutorial assumes the user input contents are irrelevant for program execution.

Use cin.get() Method to Wait for User Input

get() is std:cin member function, which works almost like an >> input operator that extracts character(s) from the stream. In this case, when we are not interested in processing the user input and only need to implement wait functionality, we can call the get function without any arguments. Notice, though, this function returns when the Enter key is pressed.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

int main()
{
    vector<char> arr = {'w','x','y','z'};
    int flag;

    flag = cin.get();

    for(auto const& value: arr)
        cout << value << "; ";
    cout << "\nDone !" << endl;

    return EXIT_SUCCESS;
}

Output:

w; x; y; z;
Done !

Use getchar Function to Wait for User Input

getchar function is the C standard library function for reading a single character from the input stream (stdin). Like the previous one, this method expects a new line character (i.e. Enter key to be pressed) to return. getchar returns eof on error or when the end of the stream is encountered.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

int main()
{
    vector<char> arr = {'w','x','y','z'};
    int flag;

    flag = getchar();

    for(auto const& value: arr)
        cout << value << "; ";
    cout << "\nDone !" << endl;

    return EXIT_SUCCESS;
}

Use getc Function to Wait for User Input

As an alternative, we could substitute the above example with the getc function. getc is passed FILE *stream argument for reading from any given input stream, but in this case, we are passing stdin, which is standard input stream commonly associated with terminal input. This function also returns when the Enter key is pressed.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

int main()
{
    vector<char> arr = {'w','x','y','z'};
    int flag;

    flag = getchar();

    for(auto const& value: arr)
        cout << value << "; ";
    cout << "\nDone !" << endl;

    return EXIT_SUCCESS;
}

Avoid Using system("pause") to Wait for User Input

system function is used to execute shell commands and the name of the command is passed as a string literal. Thus, if a pause is passed as an argument, it tries to run the corresponding command, which is only available on Windows platforms. It’s best to implement a custom wait function with methods listed above than to use a non-portable way of system("pause").

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;

int main()
{
    vector<char> arr = {'w','x','y','z'};
    int flag;

    system("pause");

    for(auto const& value: arr)
        cout << value << "; ";
    cout << "\nDone !" << endl;

    return EXIT_SUCCESS;
}

Output (on Linux platform):

sh: 1: pause: not found
w; x; y; z;
Done !
comments powered by Disqus