C++ Program for Shutting Down and Restarting a Computer

In this article, you will learn and get code to shutdown (turn off) or restart your computer system using a C++ program. Here is a list of programs that can be used to shut down or restart your computer:

Note: All programs to shutdown or restart your computer system given here are well-tested and executed. Therefore, be sure to save all your opened documents before executing these programs.

To shutdown or restart your computer system using a C++ program. Use the system() function. It is defined in the stdlib.h header file. The system() function invokes the command processor to execute a command.

The command processor for Windows-based systems is cmd (command prompt). (command prompt). The terminal is the command processor for a Linux-based system.

So to shutdown or restart your computer, place the command corresponding to shutdown or restart inside system(), as shown in the program given below.

Computer Shutdown in C++

This is the simplest program to shutdown your Windows-based computer system. In this program, we've not provided the timer. Therefore, the system gets shut down in default time, that is, 30 seconds after executing the program given below.

#include<stdlib.h>
int main()
{
    system("C:\\Windows\\System32\\shutdown /s");
    return 0;
}

After executing the above program, your computer shuts down by default after 30 seconds. As shown in the second program, you can also set the timer in seconds.

Note: Be sure to save all your documents before executing the program.

You can also write the command directly to the command prompt, like:

c++ shutdown computer

Now press the ENTER key to shut down the computer after 30 seconds, directly using cmd. But through a C++ program, we've used the system() function that invokes the command processor (cmd) to execute a command. Therefore, to execute the following command:

C:\\Windows\\System32\\shutdown /s

we've used the system() function.

In C++, turn off the computer immediately

If you want to shutdown your Windows-based system immediately, then set the timer to 0 (seconds), as shown in the program given below. The question is: write a program in C++ to shutdown the computer immediately. Here is its answer:

#include<stdlib.h>
int main()
{
    system("C:\\Windows\\System32\\shutdown /s /t 0");
    return 0;
}

Note: Here we've provided the timer as 0 to shutdown the computer in 0 seconds (or immediately).

Shut Down the Computer in the Allotted Time

This program allows the user to enter the time in seconds. For example, if the user enters 20, the system will be shut down in 20 seconds:

#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<sstream>
using namespace std;
int main()
{
    int sec, i;
    string strOne, strTwo;
    char str_One[50], str_Two[10];
    ostringstream intToStr;
    cout<<"Enter Number of Seconds: ";
    cin>>sec;
    intToStr<<sec;
    strOne = "C:\\Windows\\System32\\shutdown /s /t ";
    strTwo = intToStr.str();
    i=0;
    while(strOne[i])
    {
        str_One[i] = strOne[i];
        i++;
    }
    str_One[i] = '\0';
    i=0;
    while(strTwo[i])
    {
        str_Two[i] = strTwo[i];
        i++;
    }
    str_Two[i] = '\0';
    strcat(str_One, str_Two);
    system(str_One);
    return 0;
}

Here is its sample run:

shutdown computer c++

Now supply the input, say 20 as the timer's number of seconds, and then press the ENTER key to shutdown your computer system after 20 seconds.

Note: intToStr is a user-defined variable of the class ostringstream.

The ostringstream utput stream class is for operators on strings. Objects of this class use a string buffer that contains a sequence of characters. Basically, the ostringstream class is used to convert entered seconds (an int type value) to string.

That is, when the user enters seconds, the value gets stored in sec. and using the following statements:

intToStr<<sec;
strTwo = intToStr.str();

The strTwo now holds the same value as the sec holds, that is, the number of seconds entered by the user, but in string form.

For example, if the user enters 20 as the number of seconds, then 20 gets stored in sec, and after executing the above two lines of C++ code, strTwo=20. And after executing the following statement:

strOne = "C:\\Windows\\System32\\shutdown /s /t ";

The variable strOne now holds the command to shutdown the computer system. As a result, strOne equals C:\\Windows\\System32\\shutdown /s /t and strTwo equals 20. Now we've got to concatenate these two values and put them in the system() function to execute the complete command to shutdown the system in 20 (the given time by the user) seconds.

Because these two values belong to the string category, we must concatenate them using strcat(). But before doing this, we've got to convert it into a character array (a string). So we've copied the string into two character arrays, str_One and str_Two, one by one. That is, the string strOne gets copied to str_One, whereas strTwo gets copied to str_Two in a character-by-character manner.

Note: To learn everything there is to know about copying a string, go to Copy a String article to understand how to copy a string, why a null-terminated character gets initialized at the last index, etc.

Now we're free to apply the concatenate operation. Therefore, we've concatenated the two strings using the following statement:

strcat(str_One, str_Two);

After executing this statement, the value of str_Two gets concatenated with or appended to str_One. So now, str_One=C:\\Windows\\System32\\shutdown /s /t 20. Therefore, just pass str_One as an argument to the system() function to apply the shutdown operation.

Note: To learn more about string concatenation, refer to the concatenate string article to get all the required information.

C++ Restart Computer

Now let's restart your Windows-based system using the following C++ code: After running this program, your computer will restart in the default time (30 seconds).

#include<stdlib.h>
int main()
{
    system("C:\\Windows\\System32\\shutdown /r");
    return 0;
}

Restart your computer immediately

Similarly to how we shut down the computer immediately, we've set the timer to 0 seconds to restart the system immediately:

#include<stdlib.h>
int main()
{
    system("C:\\Windows\\System32\\shutdown /r /t 0");
    return 0;
}

In C++, restart the computer at the specified time

Follow the same program as given for shutting down the computer in the given time with only one change. That is, replace the following statement:

strOne = "C:\\Windows\\System32\\shutdown /s /t ";

with the statement given below:

strOne = "C:\\Windows\\System32\\shutdown /r /t ";

Note: In just a matter of s and r, the whole program gets changed.

Shutdown and restart the computer

Based on both the programs for shutting down and restarting computers, we've created a menu-driven program that does both the jobs of shutting down and restarting computers. That is, the user is free to enter his or her preference, or what he or she wishes to do. Either to shutdown or to restart Let's have a look at the program:

#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<sstream>
using namespace std;
int getInput();
int main()
{
    int ch, sec, i;
    string strOne, strTwo;
    char str_One[50], str_Two[10];
    ostringstream intToStr;
    cout<<"1. Shutdown Computer\n";
    cout<<"2. Restart Computer\n";
    cout<<"3. Exit\n";
    cout<<"Enter Your Choice: ";
    cin>>ch;
    if(ch==1 || ch==2)
    {
        cout<<"Enter Number of Seconds: ";
        cin>>sec;
        intToStr<<sec;
        strTwo = intToStr.str();
    }
    switch(ch)
    {
        case 1:
            strOne = "C:\\Windows\\System32\\shutdown /s /t ";
            i=0;
            while(strOne[i])
            {
                str_One[i] = strOne[i];
                i++;
            }
            str_One[i] = '\0';
            i=0;
            while(strTwo[i])
            {
                str_Two[i] = strTwo[i];
                i++;
            }
            str_Two[i] = '\0';
            strcat(str_One, str_Two);
            system(str_One);
            break;
        case 2:
            strOne = "C:\\Windows\\System32\\shutdown /r /t ";
            i=0;
            while(strOne[i])
            {
                str_One[i] = strOne[i];
                i++;
            }
            str_One[i] = '\0';
            i=0;
            while(strTwo[i])
            {
                str_Two[i] = strTwo[i];
                i++;
            }
            str_Two[i] = '\0';
            strcat(str_One, str_Two);
            system(str_One);
            break;
        case 3:
            return 0;
        default:
            cout<<"\nWrong Choice!";
            return 0;
    }
    return 0;
}

Here is the initial output produced by this program:

c++ shutdown restart computer

Now enter your choice, say 2, to restart your computer system. Here is the sample output produced after typing 2 and pressing the ENTER key:

code to shutdown restart computer c++

Now enter the number of seconds, say 50, to restart your computer system after 50 seconds.

This C++ program is created for both, i.e., to shutdown (turn off) or restart your computer. First, the program will prompt the user to choose whether to shut down or restart the computer.The first option is given to shut down the computer, whereas the second option is given to restart the computer system.

In C++, shutdown a Linux-based system

This program is created for Linux users. The system() function is also used in Linux. That is, the function is the same, but the command gets changed. Function relates to C++, whereas command relates to the local system software.

#include<stdio.h>
int main()
{
    system("shutdown -P now");
    return 0;
}

Note: For a Linux user, to shutdown a computer, you need to be logged in as root.

Set a timer before shutting down Linux

In Linux, too, you can set a timer in this way:

system("shutdown -P number of minutes");

For example,

system("shutdown -P 5");

will shutdown your system after five minutes of executing the program.

Note: In Windows-based systems, "timer refers to the number of seconds. Whereas in a Linux-based system, "timer" refers to the number of minutes.

Shutdown Windows XP-based systems in C++

This is the end of this article. It is created for Windows XP users.

#include<stdlib.h>
int main()
{
    system("C:\\Windows\\System32\\shutdown -s");
    return 0;
}

That is, instead of /s, use -s to shutdown a Windows XP-based computer system.

The same program in different languages

C++ Quiz


« Previous Program C++ Tutorial »