Shutdown and Restart Computer System

ShutDown and Restart Computer system in C++ (With source code)

 ShutDown and Restart Computer system in C++ (With source code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a ShutDown and Restart Computer system in C++ (With source code)

A ShutDown and Restart Computer initiation system in C++ is a simple program that allows the user to initiate a shutdown or restart of their computer. The program prompts the user for a choice (1 for shutdown, 2 for restart, 3 to exit) and the number of seconds to wait before executing the action.

Shutdown and Restart Computer System

It has a menu-based interface where the user can choose the desired action (shutdown or restart) and the amount of seconds to wait before performing the operation. Using the system() function, the code builds and executes the appropriate system command based on the user’s selection.

Using a C++ code to shutdown or restart your computer system. Make use of the system() method. It is specified in the header file stdlib.h. The system() function instructs the command processor to carry out a command.

cmd (command prompt) is the command processor for Windows-based computers. (A command prompt appears). To shut down or restart your machine, use the shutdown or restart command inside system(), as demonstrated in the code below.

Snake Game in C++

Objective of the project:

The objective of the above code is to create a simple program that allows the user to initiate a shutdown or restart of their computer. The code aims to provide a straightforward way for users to control their computer’s shutdown or restart process through a command-line interface.

Shutdown and Restart Computer Source Code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#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;
}

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – header files for input/output operations and string manipulation.
  • The program starts by displaying a menu of options. After the user enters their choice and the number of seconds (if applicable), the program uses a switch statement to determine the action to perform.
  • The code builds the command string to execute using the proper system command and the amount of seconds for options 1 and 2 (shutdown and restart). It uses an ostringstream object to transform the integer value of seconds to a string. The command strings are then copied character by character into matching character arrays (str_One and str_Two). The arrays are terminated with null characters, and strcat() is used to concatenate the two arrays. Finally, the command string is performed by calling the system() method.
  • For choice 3 (exit), the program simply returns 0, indicating successful termination.
  • If an invalid choice is entered, the program displays an error message and returns
  • Overall, the code provides a basic functionality to initiate shutdown or restart actions on a computer based on user input.

Creating Advanced Calculator using C++ (Source Code)

Final Output:

Shutdown and Restart Computer System

Shutdown and Restart Computer System

Video output:

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Creating a School Fees Enquiry System using C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂



Leave a Reply