. Advertisement .
..3..
. Advertisement .
..4..
How to pause a C++ program is a widely asked question among programmers. Therefore, this blog would introduce to you the most convenient methods to solve this demand. Especially, these methods can be performed whether in Windows, macOS, or Linux. Keep on reading if you want to find out the details!
How to pause a C++ program
1. Using the function getc()
The function getc() in C++ is known as a library function, which is used for reading a specific character value when the stream is input. There is only one parameter taken by this function. It is a pointer to an object of the file which identifies the input stream.
In this post, we input a type of stream as FILE* and the stream is hoped to open when using the function getc()
.
On the other hand, in Unix systems, standard streams refer to data streams that move from the executing point of a program, to the processing point of it and then move back. There are various data streams contained in your computer, but only 3 standard streams are mostly used, including stdin (standard input)
, stdout (standard output)
, and stderror (standard error)
.
To demonstrate how to pause a C++ program, we will take stdin as a corresponding argument to an input of the console.
#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!
2. Using the function std::cin::get()
In C++ language, there is a standard header for the input-output stream. You can use this header of the stream for getting input from users and export it as an output on a shell screen by containing the statement cin within the coding.
In this blog, we perform std::cin created within get()
to pause a C++ program. This method will extract the input stream characters as pointed by parameters. With the coding example below, a character is read, and the control will be returned to the program of execution.
#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;
}
3. Using the function getchar()
Alternatively, you can also pause a C++ program by using the function getchar. This function has the same features as the function getc(stdin)
, which indicates the ability to read the character after from the input stream of the console.
On the other hand, keep in mind that the return value of both functions might be EOF
, which means that the whole file has been read or there are no available characters to read. The programmer will take the responsibility for solving any arising control flow as well as returned coding errors.
#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;
}
Conclusion
Through this blog, we hope that you have figured out how to pause a C++ program. With the explanation and specific coding demonstration above, we have provided the most easy-to-understand guide for you to practice then. If you have further wonder about code handling, you can visit another post on this website for more solutions!
Leave a comment