Open In App

Pause Command in C++

pause() is a C++ method used to pause a program during execution. It allows the user to input or read data. The pause approach makes the system more readable and user-friendly by allowing the user to read the instructions before performing any task.

What is pause()?

The pause() function is used to pause the execution of the current program and wait for the user to press the enter key to terminate its operation. It serves the purpose of pausing the program, as its name implies. This method is window-specific. Only the Windows Operating System and earlier compilers like DOS support it. It can suspend a running program. To terminate the pause () method, the user can press enter or provide some time. When the pause method completes its operation, the remaining program starts to execute.



Features:

Syntax:



system(“pause”)

Here, system() is a function that calls the operating system to perform the passed task. This method is present inside the <cstdlib> header file. It invokes Windows operating system to run the pause() function.

Return Type: The pause method has no return type.

Parameters: There are no parameters in the pause () function. Users can set a timer to automatically end the program in milliseconds.

Note:

1. Run pause() code on windows only.
2. The code can also be executed on MS-DOS compiler.
3. Modern Compiler won’t support this method it throws a command not found error.

Example 1: Below is the C++ program that will display messages with the pause method.  




// C++ program to pause program 
// using pause() method
#include <iostream>
#include <cstdlib> 
using namespace std;
  
// Driver code
int main() 
{
   cout << "Welcome To Geeksforgeek ";
   system("pause");
   cout << "Done";
}

Output:

Welcome To Geeksforgeek
Press any key to continue…..
Done

Example 2: Below is the C++ program to print 1 to 10 numbers.




// C++ program to print 
// 1 to 10 numbers
#include <iostream>
#include <cstdlib> 
using namespace std;
  
// Driver code
int main() 
{
  for (int i = 1; i <= 10; i += 2) 
  {
    cout << "i = " << i << endl;
    if (i == 5) 
    {
      // Call the pause command
      cout << "Program Is Paused\n"<< endl;
      system("pause");
      cout << "Terminated\n";
    }
  }
  cout << "Done\n" << endl;
    
  return 0;
}

Output:

i = 1
i = 3
i = 5
Program Is Paused
Press any key to continue…..
Terminated
i = 7
i = 9
Done

Advantages:

Disadvantages:


Article Tags :
C++