Tutorial

Using the system("pause") command in C++

Published on August 3, 2022
author

Vijaykrishna Ram

Using the system("pause") command in C++

In this article, we’ll take a look at using the system(“pause”) command in C++.

Before going through this article, note this the system("pause") command is only available in Windows Systems.

This means that you cannot use this from any Linux / Mac machine.


The system() command

Before going through the system(“pause”) command, let’s understand what system() does.

#include <cstdlib>

int system(const char *command);

The system() function performs a call to the Operating System to run a particular command.

Note that we must include the <cstdlib> header file.

This is very similar to opening a terminal and executing that command by hand.

For example, if you want to use the “ls” command from Linux, you can use system("ls").

If you are having any Linux/Mac machine, you can try the below code.

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    // Try the "ls -l" command from your Linux / Mac machine
    int ret = system("ls -l > test.txt");
    return 0;
}

Possible Output

total 16
-rwxr-xr-x 1 2001 2000 9712 Jun 25 21:11 a.out
-rw-rw-rw- 1 2001 2000  209 Jun 25 21:11 main.cpp
-rw-r--r-- 1 2001 2000    0 Jun 25 21:11 test.txt

Now that we’re a bit clear on what system() can do, let’s look at the system(“pause”) command.


Using system(“pause”) command in C++

This is a Windows-specific command, which tells the OS to run the pause program.

This program waits to be terminated, and halts the exceution of the parent C++ program. Only after the pause program is terminated, will the original program continue.

If you’re using a Windows machine, you can run the below code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    for (int i=0; i<10; i++) {
        cout << "i = " << i << endl;
        if (i == 5) {
            // Call the pause command
            cout << "Calling the pause command\n";
            system("pause");
            cout << "pause program terminated. Resuming...\n";
        }
    }
    return 0;
}

Output - From Windows System

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
Calling the pause command
Press any key to continue . . .
pause program terminated. Resuming...
i = 6
i = 7
i = 8
i = 9

E:\Programs\sample.exe (process 14052) exited with code 0.

As you can observe, the pause command was indeed executed when our if condition i = 5.

After we hit enter, we terminated the pause program, and resumed our loop in C++ program!

Disadvantages of using the system(“pause”) command

The main pitfall of system(“pause”) is that this is platform specific. This does not work on Linux/Mac systems, and is not portable.

While this works as a kind of a hack for Windows systems, this approach may easily cause errors, when you try to run the code on other systems!

Therefore, I would suggest some other alternative ways to pause and resume a program, such as using signal handlers.


Conclusion

In this article, we learned how we could use the system(“pause”) command in C++. For similar content, do go through our tutorial section on C++ programming!

References


Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Vijaykrishna Ram

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
September 3, 2021

Thank you for this clear explanation! Though I am using codeblocks and i don’t think you need to call

- Jbfrg

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 14, 2020

    Merciii for your help

    - Mahdi Gh

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Become a contributor for community

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      DigitalOcean Documentation

      Full documentation for every DigitalOcean product.

      Resources for startups and SMBs

      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

      Get our newsletter

      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

      New accounts only. By submitting your email you agree to our Privacy Policy

      The developer cloud

      Scale up as you grow — whether you're running one virtual machine or ten thousand.

      Get started for free

      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

      *This promotional offer applies to new accounts only.