Breaking News

How to write a C++ Program that will turn off a Computer

As we know C++ is the High Level Programming Language which is most used for developing most of the Operating Systems like Microsoft and few parts of Apple Operating System. From here we got the idea why C++ Programming Language is so easy to interact with the Operating System commands.
In this article we are going to show you how to implement a Program that can turn of your Computer Safely (Shut down, Restart and Hibernate). Although from this article you will learn more things that you can use by yourself to implement other Programs.

In this program the most thing that we are going to use is the Built in Function called system(). In C++ this function is used to invoke an Operating System Commands. These are the commands that  can runs in terminal if an Operating System allows. So by saying this we get the picture that we can run any Operating System Commands  by using system() function.

To implement this program, just open your C++ Compiler and type the following codes.

#include <iostream>
using namespace std;


void shutMachine(){
    system("shutdown -s");
}

void restMachine(){
    system("shutdown -r");
}

void hiberMachine(){
    system("shutdown -h");
}


int main(){
    cout<<"Press 1 to shutdown your machine\n";
    cout<<"Press 2 to restart your machine\n";
    cout<<"Press 3 to hibernate your machine\n";
    int selct;
    char conf;
    cout<<"Enter your selection here: ";
    cin>>selct;
    switch(selct){
        case 1:
            cout<<"Are you sure you want to shutdown you Machine? [y/n]";
            cin>>conf;
            if(conf=='y'){
                shutMachine();
            }
            break;
        case 2:
            cout<<"Are you sure you want to restart you Machine? [y/n]";
            cin>>conf;
            if(conf=='y'){
                restMachine();
            }
            break;
        case 3:
            cout<<"Are you sure you want to hibernate you Machine? [y/n]";
            cin>>conf;
            if(conf=='y'){
                hiberMachine();
            }
            break;
    }
}

After typing these codes, you have to Compile to see if there is any syntax error, and finally run it.
The following picture shows how it looks in my Compiler (Dev-C++)



But if this is not enough jus watch the following video it will gives you more highlight about this Program.



For more videos tutorials, just visit my YouTube Channel by Clicking Source of Concept and please don't forget to subscribe, comment, like and share as much as you can.

We are done...!!!
I hope now you have an understanding on how we can use an Operating System Commands by using C++ Programming Language. 

Please feel free to ask any question if you didn't understand then I can reply as faster as I can. 

Please comment and share with your friends. Don't miss the following post where we will continue to discuss different things where we can get new concepts and apply them.

Thank you for reading and watching, be blessed....!!!

No comments