codescracker


c++

C++ Program to Shutdown & Restart Computer



« Previous Program C++ Tutorial »


Shutdown & Restart Computer in C++

To shutdown and restart you computer using C++ programming, just call the function system() of stdlib.h library which will call the cmd or terminal. So to shutdown and restart your computer, just place your code (including full path) to shutdown and restart your computer. Here, we provided three programs to shutdown and restart your computer. The first program is to shutdown your computer system. The second program is to restart your computer and the third program is for both shutdown and restart your computer (this program is menu-driven).

Shutdown Your Computer in C++

Following C++ program shutdown your computer system after 20 seconds.

C++ Programming Code to Shutdown Computer

/* C++ Program - Shutdown Your Computer */
		
#include<stdlib.h>
void main()
{
	
	system("c:\\windows\\system32\\shutdown /s /t 20 \n\n");
	
}

After compiling and running the above C++ program, your computer will be shutdown after 20 seconds.

Restart Your Computer in C++

Following C++ program restart your computer system after 10 seconds. That is, after 10 seconds, your computer will be shutdown and then restart.

C++ Programming Source Code to Restart Computer

/* C++ Program - Restart Your Computer */
		
#include<stdlib.h>
void main()
{
	
	system("c:\\windows\\system32\\shutdown /r /t 10 \n\n");
	
}

After compiling and running the above C++ program, your computer will restart after 10 seconds.

Shutdown then Restart Your Computer in C++

Following c++ program made for both i.e., to shutdown your computer or to restart your computer (shutdown or turn off then turn on), first it will ask to the user that either he/she want to shutdown computer or want to restart computer. Option 1 is given for shutdown computer and option 2 is given for restart computer. When the option is entered, then widows system file will be executed which is placed inside the function system() of stdlib.h that letting the system to either shutdown computer or restart your computer. The delay to shutdown computer or restart computer is 20 second. This time is taken by the system to shutdown computer or restart computer. But you can change this time like you can make time 0 to shutdown computer or restart computer immediately or you can increase the time to make shutdown computer or restart computer after the given time.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	int choice;
	cout<<"1. Shutdown Your Computer \n";
	cout<<"2. Restart Your Computer \n";
	cout<<"\n Enter your choice : ";
	cin>>choice;
	switch(choice)
	{
		case 1 : cout<<"Your system will be shutdown after 20 seconds \n";
				system("c:\\windows\\system32\\shutdown /s /t 20 \n\n");
				break;
		case 2 : cout<<"Your system will be restarted in 20 seconds\n";
				system("c:\\windows\\system32\\shutdown /r /t 20\n\n");
				break;
		default : cout<<"Wrong Choice..!!\n";
	}
	getch();
}

Here if you will choose 1st option, then your system will be shutdown after 20 seconds, and if you will choose 2nd option, then your system will be restarted after 20 seconds (it first shutdown after 20 seconds, then start )

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program C++ Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test