C++

System Pause C++

The function system (“pause”) is utilized to stop the program at any time or the termination of the code and obtain the outcome of the code on the console terminal. The function system pause in C++ is typically utilized when the user desires to get the outcome in a console window. This supports the user in fixing the code in a good mode and allows the user to get the resultant values at different program phases. In C ++, we utilize the system (“pause”) to implement the operating system’s pause command in the program. Therefore, the user is prompted to tap any key to carry on. If we cannot utilize system pause C++, we may use cin.get() that waits for us to tap any key. When we utilize the system (“pause”) command, it does not work on Linux operating system or Mac. It only works on Windows operating systems. In this article, we discuss the system pause command in C++.

For running the code, we install DEVC++. To run the codes, tap the button F11 from the keyboard.

Usage of System (“Pause”) Command:

The system (“pause”) command is used to execute the pause code. The code is waiting to finish and will stop running the parent C ++ code. The original code will only continue after the pause code ends. If we use a Windows operating system, we can run the following program.

In this example, we utilize two header files: #include <iostream> and #include <cstdlib>. To utilize the system (“pause”) command in the program, we must include the “#include <cstdlib>” header file at the start of the program.

Before decoding a program into machine language, the compiler carries out the header files. Next, we use the main() function. Here, the “For” loop contains three statements. The variable used inside the loop is “k.” We initialize the variable “k” to 1. Then, we apply the test condition k<8, it tests the loop every time to observe if k is less than 8. If the defined condition is true, the loop body is implemented. If the condition is false, the loop ends and moves on to the next statement. This completes the entire program:

#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
    for (int k=1; k<8; k++) {
        cout << "k = " << k << endl;
        if (k == 3) {
       
            cout << "Call the pause program\n";
            system("pause");
            cout << "the pause program is terminated. Resuming...\n";
        }
    }
    return 0;

The final statement k++ increments the variable “k” every time the loop is implemented. Even when the “for” loop ends, the variable “k” in the loop is well-defined and has the values assigned in the last increment. Cout is an output function. The double quotation marks surround the message we want to print. The statements in the program end with a semicolon. So, a semicolon is utilized at the end of the cout statement:

As we see, the code is executed, and the first three values of “k” are shown as an output. The system (“pause”) command executes. When we pressed the enter key to continue, it exited the paused code and continued the loop in the code. And by this, we get the next 4 values of k.

Using Cin.get() Function

Cin.get() function is one of the alternatives existing for the system function (“pause”). It will break the execution of the program when needed. After execution, the cin.get() method waits for user input before continuing. As soon as we enter the input, the program will continue to run. This method is helpful if there is a need to enter a value in the code during implementation. This function is a program-level method, and it does not call the operating system to implement the commands. It is a standard library function, so we don’t need to explicitly add a distinct header file. We utilize the cin.get() function as shown below:

#include<iostream>
using namespace std;
int main()
{
    int Values[10] = { 30, 50, 70, 90, 110, 120, 140, 160, 180, 210 };
   
    for (int j=0; j<10; j++)
    {
        if ( Values[j]== 160)
        {      
            cout << "Number 160 is present at array position: " << j;
       
        cin.get();
        }
    }
}

First, we add a header file in the program. We apply the main function. We take any 10 random numbers and generate an array of these numbers. The variable used inside the loop is “j”. First, we initialize the variable and then apply the test condition. The variable “j” gives the value until it satisfies the given condition. We want to know the position of the value “160”. We utilize the cout function. The message we want to print is “number 160 is present at array position”. In the end, we utilize the cin.get() function:

As the number 160 is present at the 8th position in the array, we get the output 7 because the index of the array starts with 0. So, the digit present at the 8th index shows the 7th position.

System() Function:

The system() is a predefined usual library function. We pass input commands to the system() function, then these commands will be implemented on the operating system terminal. This function calls the Operating System to execute a specific command. This may be very much like launching a terminal and implementing the command with the aid of using a hand:

#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
    if (system(NULL))
       cout << "Command processor is running";
    else
       cout << "Command processor is not running";
 
    return 0;
}

It is a common approach to test if we can run instructions using a system() in an Operating System. In this program, we should encompass the header file <cstdlib>. We include the header file <iostream>. These header files are applied at the start of the code. We apply the if-else condition. Inside the condition, we utilize the system() function. When we pass a parameter null pointer to the system() function instead of a string, the system() function returns the statement that the command processor is running. Otherwise, the command processor is not running.

Conclusion:

In the article, we talked about system pause C++. We see the program utilizing the system (“pause”) command. It is used to run the pause commands. If we are not sure to use the system (“pause”), then we use the cin.get() function. It also waits for us to enter any value. We have also discussed the system() function. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.