C++ Tutorials

C++ Tutorials

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. Must include the <cstdlib> header file.

#include<bits/stdc++.h>
#include <iostream>
#include <cstdlib>

using namespace std;
 
int main() {
    // Try "ls -l" command from your Linux / Mac machine
    int ret = system("ls -l > test.txt");
    return 0;
}
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

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

System(Pause C++ Library)

#include<bits/stdc++.h>
#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.

The compiler to include the iostream library files these are all the code that’s required to input and output things in a C++ program and almost every program requires.

That this is followed by using namespace standard standard is a namespace or an area where a lot of things that you need to write simple C++ programs are located.

So those first two lines need to go in your program this is followed by what’s called the main program it is a function and it’s required if in order to run a C++ program a main function must exist.

And the main program or the main function the body of it is enclosed in opening and closing curly brace so inside the main program we’ll see an output statement.

We’re going to be talking more about these output statements in this chapter that’s followed by a system pause and return zero.

I’ll be demonstrating what a system pause does in a minute and returning zero just means there was normal termination to this program so every program you write is going to look like this what’s going to change is the highlighted area.

Here you’ll have different statements inside the main body of the program now I’m going to run my program you could choose to bug start debugging or you can just hit the f5 key that’s a shortcut key or you can just click on this green triangle icon here.

It’ll say the project is out of date and that’s just because I just made changes to my source code so it is out of date would you like to build it yes and my program is going to run and all it does is it says hello there on the screen and press any key to continue.

So I’ll press a key to continue and what I want to do is demonstrate this system pause state if I take this out and I run my program again the program will run quickly and the window will close automatically.

So the system pause is actually keeping the output window open so you have two choices you can instead choose to bug start without debugging which is ctrl f5 and that will stop the output window or since programming students generally like to just click on this icon.

We have to put a system pause here in the textbook you will notice that the author does not put a system pause and that’s probably likely because he’s choosing control f5 if he’s using Visual Studio.

So the only other thing I’d like to show you as generally programs will start with what’s referred to as a comment and a comment here starts with two slashes and this is where you can just put text in any text that you want it’ll be ignored by the compiler a lot of times.

The programmer will put their name here and so that’s just an example of a comment and you’ll notice that the comments are in green so that’s just the structure of a basic C++ program.

System(pause c++ example)

#include <Windows.h>

int main() {
	system("Pause");
}

System(pause in c++ header file)

Must include the <cstdlib> header file.

// A C++ system(pause in c++ header file) 
#include <bits/stdc++.h>
#include<cstdlib>
#include<iostream>
using namespace std; 
int main () 
{ 
	char filename[100]; 
	cout << "Enter file name to compile "; 
	cin.getline(filename, 100); 
	string str = "gcc "; 
	str = str + " -o a.out " + filename; 
	const char *command = str.c_str(); 

	cout << "Compiling file using " << command << endl; 
	system(command);
        cout << "\nRunning file "; 
	system("./a.out"); 

	return 0; 
} 

System(cls c++)

“cls” Means , clear screen, Clear the console/terminal.

#include <cstdlib>


#include<stdlib.h>
#include <stdio.h>

#include <time.h>
void screen_clear()
{
  system ( "CLS" );
}
int main()
{
  clear();
}
cout<< "Good-Bye"<<endl;
  • MS-DOS based: system(“cls”);
  • Unix based: system(“clear”);

System(pause alternative c++)

int PauseConsole( int ReturnCode )
{
  std::cin.ignore( ( std::numeric_limits< std::streamsize >::max )( ), '\n' );
  return( ReturnCode );
}
int main( )
{

  return( PauseConsole( 0 ) );
}

System(pause c++ linux)

std::cout << "Press \'Return\' to end." << std::endl;
std::cin.flush();
std::cin.get();

System(pause c++ mac)

#include <stdlib.h>
int main()
{
system( “read -n 1 -s -p \”Press any key to continue…\”” );
return 0;
}