HOWTO · C++
How to Pause a Program in C++
This article demonstrates how to pause a program in C++.
On this page
There are two different reasons to pause a C++ program: wait until a person provides input, or wait for a specified duration. Use an input wait for a console prompt such as “Press Enter to continue.” Use std::this_thread::sleep_for when the program should continue after a timed delay without user input. These approaches are not interchangeable: redirected input may end immediately, while a timed delay does not let a person control when execution resumes.
Choose between waiting for input and a timed delay
Choose a standard-input function when a person should decide when the program continues. getc(stdin), std::cin.get(), and getchar() all read from standard input. In an interactive terminal, they typically wait for input; the exact behavior of Enter depends on the terminal’s input mode. Choose std::this_thread::sleep_for for a duration-based wait in an application flow, such as allowing time between operations. The delay is a minimum requested waiting period, not a guarantee that execution resumes at an exact instant.
Wait for console input with getc(), std::cin.get(), or getchar()
For C++ streams, std::cin.get() is a direct way to read one character. The C library alternatives are getc(stdin) and getchar(); the latter reads from stdin implicitly. Each returns an integer so it can represent either a character value or EOF. Check the return value when the program must distinguish a character from end-of-file or an input error. These calls do not inherently mean “wait for Enter”: they read one character from the input stream, and terminal buffering determines when typed input becomes available.
If the user types a line and presses Enter, a terminal may make the line’s characters available together. A single-character read can consume only the first character, leaving the rest, including the newline, for a later read. If the program needs a full line, use std::getline instead. Avoid mixing std::cin reads with C stdio reads from the same input stream unless stream synchronization and buffering are handled deliberately.
Pause on Windows with std::system(“pause”)
std::system("pause") asks the operating system command processor to run a command named pause. It is commonly available in Windows command environments, where it displays a prompt and waits for a key. It is not a standard C++ input API and is not portable: other command processors may not provide that command, and the call can fail. Use it only when the program is intentionally tied to a compatible Windows environment. For portable console input, prefer std::cin.get(); for an automatic delay, use std::this_thread::sleep_for.
Add a timed delay with std::this_thread::sleep_for
Include <chrono> to specify a duration and <thread> for std::this_thread::sleep_for. The following complete example requests a two-second delay between two messages. It does not wait for a keypress.
Run this example:
#include <chrono>
#include <iostream>
#include <thread>
int main() {
std::cout << "Starting timed pause.\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Finished.\n";
return 0;
}
Use std::chrono::milliseconds(250) for a quarter-second interval, or use another chrono duration that matches the task. The thread may remain asleep longer than requested if the operating system schedules it later. Do not describe this function as sleep(); the C++ call shown here is std::this_thread::sleep_for.
Handle end-of-file and portability limits
Standard-input functions can return EOF when input has ended or an error occurs, rather than waiting for a character. This often matters when a program is launched with input redirected from a file or pipe, or when a runner supplies no interactive input. In that situation, a pause based on getc(stdin), std::cin.get(), or getchar() may continue immediately. Check the result and decide how the application should respond; repeatedly reading after EOF does not create a useful pause.
For a console-only “press Enter” prompt, read a line with std::getline if consuming the whole line is appropriate, and handle a failed read as end-of-input. For a fixed delay, use std::this_thread::sleep_for. Keep std::system("pause") restricted to environments that provide the command. Selecting based on whether the program needs human input, elapsed time, or platform-specific console behavior avoids surprises when the same program runs in a different terminal or as a non-interactive process.