How to Pause a Program in C++

  1. Use getc() Function to Pause the Program
  2. Use std::cin::get() Method to Pause the Program
  3. Use getchar() Function to Pause the Program

This article will explain several methods of how to pause a program in C++.

Use getc() Function to Pause the Program

The getc() function is from the C standard input-output library, and it reads the next character from the given input stream. The input stream is of type FILE*, and the function expects the stream to be opened. On almost all Unix systems, during the program startup, 3 standard file streams are opened - namely: stdin, stdout and stderr. In the following example, we pass stdin as an argument, which corresponds to a console input, to wait for a user to continue program execution.

#include <iostream>
#include <thread>
#include <chrono>

using std::cout;
using std::endl;
using std::copy;
using std::this_thread::sleep_for;
using namespace std::chrono_literals;

int main()
{
    int flag;
    cout << "Program is paused !\n" <<
        "Press Enter to continue\n";

    // pause the program until user input
    flag = getc(stdin);

    cout << "\nContinuing .";
    sleep_for(300ms);
    cout << ".";
    cout << ".";
    cout << ".";
    cout << "\nProgram finished with success code!";

    return EXIT_SUCCESS;
}

Output:

Program is paused !
Press Enter to continue

Continuing ....
Program finished with success code!

Use std::cin::get() Method to Pause the Program

Another method to pause a program is to call std::cin builtin method get, which extracts characters from the input stream as specified by parameters. In this case, we just read a single character and return control to the executing program.

#include <iostream>
#include <vector>
#include <thread>
#include <chrono>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;
using std::this_thread::sleep_for;
using namespace std::chrono_literals;

int main()
{
    int flag;
    cout << "Program is paused !\n" <<
        "Press Enter to continue\n";

    // pause the program until user input
    flag = cin.get();

    cout << "\nContinuing .";
    sleep_for(300ms);
    cout << ".";
    cout << ".";
    cout << ".";
    cout << "\nProgram finished with success code!";

    return EXIT_SUCCESS;
}

Use getchar() Function to Pause the Program

As an alternative way, we can reimplement the same functionality with the getchar function call. getchar is the equivalent call to getc(stdin), which reads the next character from the console input stream.

Note that both functions may return EOF, indicating that the end of the file has been reached i.e. no characters are available to read. The programmer is responsible for handling any exceptional control flow and returned error codes.

#include <iostream>
#include <vector>
#include <thread>
#include <chrono>

using std::cout; using std::cin;
using std::endl; using std::vector;
using std::copy;
using std::this_thread::sleep_for;
using namespace std::chrono_literals;

int main()
{
    int flag;
    cout << "Program is paused !\n" <<
        "Press Enter to continue\n";

    // pause the program until user input
    flag = getchar();

    cout << "\nContinuing .";
    sleep_for(300ms);
    cout << ".";
    cout << ".";
    cout << ".";
    cout << "\nProgram finished with success code!";

    return EXIT_SUCCESS;
}