C++ is fun ,C++ can do everything.
Let's see how we can shutdown,restart or log off in Windows OS.
First of all, we need to get familiar with system() function in C++. This function is present in the "stdlib.h" library.
The system() function allows C++ programs to run another program by passing a command line (pointed to by string) to the operating system's command processor that will then be executed.
Syntax: int system(const char* command)
Now, to shut down, restart or log off windows, we can execute the shutdown.exe present in windows\system32 folder
with specific commands using the system() function in C++
1) Shutdown : c:\\windows\\system32\\shutdown /s
2) Restart : c:\\windows\\system32\\shutdown /r
3) Log off : c:\\windows\\system32\\shutdown /l
Here's a menu-driven program to implement the above idea :
#include <iostream>
#include <stdlib.h>
using namespace std;
signed main()
{
cout<<"-------Welcome-------"<<endl;
cout<<"[S]hutdown"<<endl; // enter S to shutdown
cout<<"[R]estart"<<endl; // enter R to restart
cout<<"[L]ogoff"<<endl; // enter L to logoff
cout<<"[E]xit"<<endl; // or else enter E to exit
char choice; //to store the choice of user
cin>>choice;
if(choice=='S' || choice=='s'){
cout<<"Shutting down..."<<endl;
system("c:\\windows\\system32\\shutdown /s"); // using system function to shutdown
}
else if(choice=='R' || choice=='r'){
cout<<"Restarting..."<<endl;
system("c:\\windows\\system32\\shutdown /r"); // using system function to restart
}
else if(choice=='L' || choice=='l'){
cout<<"Logging off..."<<endl;
system("c:\\windows\\system32\\shutdown /l"); // using system function to log off
}
else if(choice=='E' || choice=='e'){
cout<<"Exiting..."<<endl;
exit(0);
}
else
cout<<"Command not valid. Enter valid command"<<endl;
}
Comments