Program to Shutdown and restart in C++

Program to Shutdown and restart

To shut down or restart your computer, you can use the system() function from the stdlib.h library by providing the appropriate command along with the path to the relevant executable.

Algorithm

  1. Display a menu to the user with options for shutting down the computer, restarting the computer, and exiting the program.
  2. Prompt the user to select an option from the menu.
  3. Use a switch statement to handle the user's selection:
    • If the user selects the option to shut down the computer, execute the command system("C:\\windows\\system32\\shutdown /s /t 30 \n\n").
    • If the user selects the option to restart the computer, execute the command system("C:\\windows\\system32\\shutdown /r /t 30\n\n").
    • If the user selects the option to exit the program, exit the program using the exit function.
  4. The system will perform the chosen action.

Code

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int choice;

    cout << "1. Shutdown Your Computer \n";
    cout << "2. Restart Your Computer \n";
    cout << "3. Exit\n";
    cout << "\nEnter your choice: ";
    cin >> choice;

    switch (choice)
    {
        case 1:
            cout << "System will shutdown after 30 seconds \n";
            system("C:\\windows\\system32\\shutdown /s /t 30 \n\n");
            break;

        case 2:
            cout << "System will restart in 30 seconds\n";
            system("C:\\windows\\system32\\shutdown /r /t 30\n\n");
            break;

        case 3:
            exit(0);
            break;

        default:
            cout << "Wrong Choice!!\n";
            break;
    }

    return 0;
}

Output

If the user enters 1, the output will be:
System will shutdown after 30 seconds 

If the user enters 2, the output will be:
System will restart in 30 seconds

If the user enters 3, there will be no output, as the program will simply exit.

If the user enters any other value (e.g., 4), the output will be:
Wrong Choice!!