Wait for User Input in C++

Wait for User Input in C++

Pausing a Program to Wait for User Input in C++

In this tutorial, we will discuss different methods to wait for user input in C++. To implement this, we will get some user input in the middle of the program using different functions. Waiting for user input will pause the execution of the program.

Wait for Keypress in C++

In general, the methods implemented in this article are indirectly also simulating wait for keypress in C++. This is because we actually need to press the Enter key for the program execution to resume. The functions which take input from the standard input stream, require the user to enter some character and press Enter to resume program execution. The system() function discussed below is the direct way to wait for keypress in C++, because here pressing any key will resume execution.

Ways to Wait for User Input in C++

Let us now discuss all the possible methods to wait for user input in C++.

Using the system() Function to Wait for User Input in C++

We can call a system command in a C++ program using the system() function. By providing the pause argument within the function the program will pause and wait for a keypress to be initiated to continue execution. This method is a good way to simulate wait for a keypress in C++.

See the code below.

The above code will also display "Press any key to continue" in the console, making it a good choice to simulate wait for keypress in C++.

The only limitation of this method is that it works for Windows only. We can use the read argument with this function to make it work on Mac and Linux.

Using the cin.get() function to wait for user input in C++

We use the cin.get() function to access a character from the input stream. Unlike the >> operator, the cin.get() function does not terminate on encountering a whitespace character.

We can use this function without any argument to make the program wait for some user input.

For example,

Output:

Before
“user input”
After

Using the getc() Function to Wait for User Input in C++

The getc() function reads the next character from a given input stream. By providing the stdin argument in the function, we can make it read from the standard input. By this, we can make the program wait for user input in C++.

For example,

Output:

Before
“user input”
After

Using the fgetc() Function to Wait for User Input in C++

The fgetc() function is similar to the getc() function. The main difference is that this function is a little slow as compared to its predecessor and we cannot implement it as a macro in C++.

To make a program wait for user input, we will pass the standard input stream as an argument in the function, as we did earlier.

For example,

Output:

Before
“user input”
After

Using the getchar() Function to Wait for User Input in C++

We can use the getchar() function to get the net character from the standard input stream in C++.

This method is equivalent to the getc(stdin) or the fgetc(stdin) functions discussed previously.

For example,

Output:

Before
“user input”
After

Conclusion

We discussed several methods to wait for user input in C++. The system() function will require different commands for different operating systems. It also directly simulates wait for keypress in C++. The other functions will work to get the required input from the standard input stream.

That’s all how to wait for user input in C++.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *