Tutorials  /  Linux Basics

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

Ccentron Redaktion · January 2024 ·3 min read ·Linux Basics, Tutorial

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

Important Note

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.

Code
#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 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").

Example for Linux/Mac

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

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

Code
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
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

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 execution of the parent C++ program. Only after the pause program is terminated, will the original program continue.

Example for Windows

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

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

Code
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.

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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren