Open In App

Pause Command in C++

Last Updated : 16 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Window specific
  • Supported by the older compiler
  • Fast and Efficient

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++




// 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++




// 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:

  • Users can easily remember this method. It is useful to pause programs in older compilers like MS-DOS. 
  • To pause programs in the Windows system. 
  • The user can pause the program for as long as he wants by passing the time limit.

Disadvantages:

  • The pause() function is not portable and is platform-specific. As the pause () method is window-specific, it will not work on other operating systems like Unix, Linux, etc.
  • When the user calls the pause method, he also calls the system function. which all takes extra memory 
  • The remaining program will not execute until the pause () method is terminated or the user presses Enter.
  • It’s slow. 
  • It’s insecure.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads