Program to Shutdown and restart Computer

0 Suresh Chand March 31, 2021

In this example, We will write a program using C++ to shutdown and restart computer. We will use the system function which is in stdlib.h header file.

For the program to shutdown and restart computer, We will use the following commands that we will use the system function which is in stdlib.h header file.

shutdown /s /t timeout_in_seconds

To restart a computer, You can use following commands

shutdown /r /t timeout_in_seconds

You can check the cmd commands for more details. When you execute the following commands then, You will see the popup as below image

C plus program to shutdown and restart computer
Program to shutdown and restart computer

Lets look and example to make clear concepts

Program to shutdown and restart computer

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()

{

	int choice;

    cout << "Choose one of the option from menu: \n";

	cout << "1. Shutdown Your Computer \n";

	cout << "2. Restart Your Computer \n";

	cout << "3. Exit\n";

	cout << "\n Enter 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);

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

	return 0;

}

When you run this program then,

Choose one of the option from menu: 
1. Shutdown Your Computer
2. Restart Your Computer
3. Exit

First you will see above output then you have to enter of the option.

  • When you press 1 then your computer will automatically shutdown
  • When you press 2 then your computer will start to restart
  • When you press 3 then the program will exit
  • When you press the other numbers then It will show the message “Wrong Choice!!”

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

Ads Block Detector Powered by codehelppro.com

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System