System Pause C++ Command Guide

Are you looking for a way to use System Pause in C++? In this article, I’ll show you how to use the system(“pause”) command in C++ to pause your program’s execution.

Before you learn about the system(“pause”), let us discuss the system() function to understand how it works.

What is system() in C++?

The system() is a function or method present in both stdlib.h and cstdlib.h that is used to invoke an operating system[1] command from a C++ program. It is defined in the standard library header files stdlib.h and cstdlib.h.

Syntax:

int system(const char *command);

To list the contents of a directory on Windows, for example, we can use the system(“dir”) command, and on Linux, we can use the system(“ls”) command.

Simple Program using System Pause in C++

Let us take a simple C++ program to understand how can we use the system(“pause”). As in the below program, we are going to an array of numbers from 1 to 10 using for loop.

// C++ program that pauses the Window once value of i reaches 5.
#include <iostream>
using namespace std;
int main ()
{
    for(int i=0;i<10;i++){
      cout<<"value of i is "<<i<<endl;
      if(i==3){
        cout<<"Calling System Pause Here."<<endl;
        system("pause");
       }
      }
    return 0;
}

Output:

System Pause C++ Command Guide

Disadvatages of System Pause Usage in C++

This application is OS-dependent and employs a series of complex processes.

It pauses your software while calling the operating system to launch the operating system shell. The operating system detects the pause and allocates the necessary memory to carry out the task. The software then deallocates memory, exits the Operating System, and resumes the program.

Hence instead of using the system(“pause”), you can use another command such as cin.get(). Note that this command will only work in C++. Since this is defined natively in C++ and is easy to use.

Using cin.get() instead of system(“pause”)

Let us see the example c++ code for system pause in c++. It is mostly advisable to use the natively developed function by c++ than the system commands.

// C++ program that pauses the Window once value of i reaches 5.
#include <iostream>
using namespace std;
int main ()
{
    for(int i=0;i<10;i++){
      cout<<"value of i is "<<i<<endl;
      if(i==5)
        cin.get()
      }
    return 0;
}

Output:

System Pause C++ Command Guide

As a result, we can see that both system(“pause”) and cin.get() are waiting for a key to be pressed, but cin.get() is not dependent on the operating system and does not follow the procedures to pause the application described previously.

Wrap Up

I hope you understand the concept of system pause in C++ and when this system pause can be used for the command line.

If you have a better explanation than the one above, please let me know in the comments section and I will gladly add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Check If Vector Contains A Given Element in C++
  2. How To Declare An Empty Array in C++
  3. How To Convert Char to Int in C++
  4. How To Find The Sum Of An Array Of Numbers in JavaScript
  5. Easiest and Best Way To Convert Int to String in C++

Leave a Comment