codescracker
c++

C++ Program to Shutdown & Restart Computer



« Previous Program C++ Tutorial »

In this article, you will learn and get code to shutdown (turn off) or restart your computer system using a C++ program. Here are the list of programs on shutdown or restart your computer system:

Note - All programs on 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 system() function. It is defined in stdlib.h header file. The system() function invokes the command processor to execute a command.

The command processor for windows based system is cmd (command prompt). Whereas the command processor for linux based system is terminal.

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

Shutdown Computer 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 shutdown in default time, that is, in 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 system gets shutdown after 30 seconds by default. You can also define the timer, in number of seconds as shown in the program given below (the second program).

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 ENTER key to shutdown the computer after 30 seconds, directly using cmd. But through a C++ program, we've used 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 system() function.

Shutdown Computer Immediately

If you want to shutdown your windows based system immediately, then provide the timer as 0 (second) as shown in the program given below. The question is, write a program in C++ to shutdown 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 second (or immediately).

Shutdown Computer in Given Time

This program allows user to enter the timer in seconds. For example, if user enters 20, then system gets shutdown after 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 timer (number of seconds), and then press ENTER key to shutdown your computer system after 20 seconds.

Note - The intToStr is a user-defined variable, of the class ostringstream

The ostringstream output stream class to operator on string. Objects of this class uses 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 user enters seconds, then the value gets stored in sec. And using the following statements:

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

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

For example if user enters 20 as 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. Therefore strOne = C:\\Windows\\System32\\shutdown /s /t and strTwo=20. Now we've have to concatenate these two values and put in the system() function to execute the complete command to shutdown the system in 20 (given time by user) seconds.

Because these two values comes in the category of string, therefore using strcat() we've to concatenate these two. But before doing this, we've to convert it into character array (string). So we've copied the string into character array, 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 character by character manner.

Note - To get its in-depth detail about copying string, refer to copy a string to get every required thing about the topic. Like how to copy string, why a null terminated character gets initialized at last index etc. like things.

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

strcat(str_One, str_Two);

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

Note - To learn more about string concatenation, refer to concatenate string article to get every required thing.

Restart Computer in C++

Now let's restart your windows based system using following C++ code. This program restarts your computer system in default time (30 seconds), after executing it.

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

Restart Computer Immediately

In similar way as done in shutdown computer immediately, here also we've written the timer as 0 seconds to restart the system immediately:

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

Restart Computer in Given Time

Note - Follow the same program as given for shutdown computer in 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 - Just a matter of s and r, the whole program gets changed.

Shutdown and Restart Computer

Based on both the programs of shutdown and restart, we've created a menu-driven program that does both the job of shutdown and restart computer. That is, user is allowed to enter his/her choice, that what he/she wants 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 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 to restart your computer. First the program will ask from user, either he/she wants to shutdown or to restart computer. The first option is given to shutdown, whereas the second option is given to restart the computer system.

Shutdown Linux Based System

This program is created for Linux user. In linux also, system() function is used. That is, the function is same, but the command gets changed. Because 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 linux user, to shutdown computer, you need to be logged in as root user.

Set Timer before Shutdown for Linux User

In linux also, you can set timer in this way:

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

For example,

system("shutdown -P 5");

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

Note - In Windows based system, timer refers to number of seconds. Whereas in linux based system, timer refers to number of minutes.

Shutdown Windows XP based System

This is the last program of this article. It is created for Windows XP user:

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

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

Same Program in Other Languages

C++ Online Test


« Previous Program C++ Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker