- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Program for Shutting Down and Restarting a Computer
In this article, you will learn and get code to shutdown (turn off) or restart your computer system using a C++ program. Here is a list of programs that can be used to shut down or restart your computer:
- Shutdown the computer system at the default time
- Turn off the computer immediately
- At Run-Time, the user can shutdown the computer at any time
- Restart the computer at the default time
- Restart your computer immediately
- Restart the computer at the specified time
- Menu-driven program to shut down or restart the computer
- Shutdown the Linux-based system
- Shutdown your Windows XP-based computer
Note: All programs to shutdown or restart your computer system given here are well-tested and executed. Therefore, be sure to save all your opened documents before executing these programs.
To shutdown or restart your computer system using a C++ program. Use the system() function. It is defined in the stdlib.h header file. The system() function invokes the command processor to execute a command.
The command processor for Windows-based systems is cmd (command prompt). (command prompt). The terminal is the command processor for a Linux-based system.
So to shutdown or restart your computer, place the command corresponding to shutdown or restart inside system(), as shown in the program given below.
Computer Shutdown in C++
This is the simplest program to shutdown your Windows-based computer system. In this program, we've not provided the timer. Therefore, the system gets shut down in default time, that is, 30 seconds after executing the program given below.
#include<stdlib.h> int main() { system("C:\\Windows\\System32\\shutdown /s"); return 0; }
After executing the above program, your computer shuts down by default after 30 seconds. As shown in the second program, you can also set the timer in seconds.
Note: Be sure to save all your documents before executing the program.
You can also write the command directly to the command prompt, like:
Now press the ENTER key to shut down the computer after 30 seconds, directly using cmd. But through a C++ program, we've used the system() function that invokes the command processor (cmd) to execute a command. Therefore, to execute the following command:
C:\\Windows\\System32\\shutdown /s
we've used the system() function.
In C++, turn off the computer immediately
If you want to shutdown your Windows-based system immediately, then set the timer to 0 (seconds), as shown in the program given below. The question is: write a program in C++ to shutdown the computer immediately. Here is its answer:
#include<stdlib.h> int main() { system("C:\\Windows\\System32\\shutdown /s /t 0"); return 0; }
Note: Here we've provided the timer as 0 to shutdown the computer in 0 seconds (or immediately).
Shut Down the Computer in the Allotted Time
This program allows the user to enter the time in seconds. For example, if the user enters 20, the system will be shut down in 20 seconds:
#include<iostream> #include<stdlib.h> #include<string.h> #include<sstream> using namespace std; int main() { int sec, i; string strOne, strTwo; char str_One[50], str_Two[10]; ostringstream intToStr; cout<<"Enter Number of Seconds: "; cin>>sec; intToStr<<sec; strOne = "C:\\Windows\\System32\\shutdown /s /t "; strTwo = intToStr.str(); i=0; while(strOne[i]) { str_One[i] = strOne[i]; i++; } str_One[i] = '\0'; i=0; while(strTwo[i]) { str_Two[i] = strTwo[i]; i++; } str_Two[i] = '\0'; strcat(str_One, str_Two); system(str_One); return 0; }
Here is its sample run:
Now supply the input, say 20 as the timer's number of seconds, and then press the ENTER key to shutdown your computer system after 20 seconds.
Note: intToStr is a user-defined variable of the class ostringstream.
The ostringstream utput stream class is for operators on strings. Objects of this class use a string buffer that contains a sequence of characters. Basically, the ostringstream class is used to convert entered seconds (an int type value) to string.
That is, when the user enters seconds, the value gets stored in sec. and using the following statements:
intToStr<<sec; strTwo = intToStr.str();
The strTwo now holds the same value as the sec holds, that is, the number of seconds entered by the user, but in string form.
For example, if the user enters 20 as the number of seconds, then 20 gets stored in sec, and after executing the above two lines of C++ code, strTwo=20. And after executing the following statement:
strOne = "C:\\Windows\\System32\\shutdown /s /t ";
The variable strOne now holds the command to shutdown the computer system. As a result, strOne equals C:\\Windows\\System32\\shutdown /s /t and strTwo equals 20. Now we've got to concatenate these two values and put them in the system() function to execute the complete command to shutdown the system in 20 (the given time by the user) seconds.
Because these two values belong to the string category, we must concatenate them using strcat(). But before doing this, we've got to convert it into a character array (a string). So we've copied the string into two character arrays, str_One and str_Two, one by one. That is, the string strOne gets copied to str_One, whereas strTwo gets copied to str_Two in a character-by-character manner.
Note: To learn everything there is to know about copying a string, go to Copy a String article to understand how to copy a string, why a null-terminated character gets initialized at the last index, etc.
Now we're free to apply the concatenate operation. Therefore, we've concatenated the two strings using the following statement:
strcat(str_One, str_Two);
After executing this statement, the value of str_Two gets concatenated with or appended to str_One. So now, str_One=C:\\Windows\\System32\\shutdown /s /t 20. Therefore, just pass str_One as an argument to the system() function to apply the shutdown operation.
Note: To learn more about string concatenation, refer to the concatenate string article to get all the required information.
C++ Restart Computer
Now let's restart your Windows-based system using the following C++ code: After running this program, your computer will restart in the default time (30 seconds).
#include<stdlib.h> int main() { system("C:\\Windows\\System32\\shutdown /r"); return 0; }
Restart your computer immediately
Similarly to how we shut down the computer immediately, we've set the timer to 0 seconds to restart the system immediately:
#include<stdlib.h> int main() { system("C:\\Windows\\System32\\shutdown /r /t 0"); return 0; }
In C++, restart the computer at the specified time
Follow the same program as given for shutting down the computer in the given time with only one change. That is, replace the following statement:
strOne = "C:\\Windows\\System32\\shutdown /s /t ";
with the statement given below:
strOne = "C:\\Windows\\System32\\shutdown /r /t ";
Note: In just a matter of s and r, the whole program gets changed.
Shutdown and restart the computer
Based on both the programs for shutting down and restarting computers, we've created a menu-driven program that does both the jobs of shutting down and restarting computers. That is, the user is free to enter his or her preference, or what he or she wishes to do. Either to shutdown or to restart Let's have a look at the program:
#include<iostream> #include<stdlib.h> #include<string.h> #include<sstream> using namespace std; int getInput(); int main() { int ch, sec, i; string strOne, strTwo; char str_One[50], str_Two[10]; ostringstream intToStr; cout<<"1. Shutdown Computer\n"; cout<<"2. Restart Computer\n"; cout<<"3. Exit\n"; cout<<"Enter Your Choice: "; cin>>ch; if(ch==1 || ch==2) { cout<<"Enter Number of Seconds: "; cin>>sec; intToStr<<sec; strTwo = intToStr.str(); } switch(ch) { case 1: strOne = "C:\\Windows\\System32\\shutdown /s /t "; i=0; while(strOne[i]) { str_One[i] = strOne[i]; i++; } str_One[i] = '\0'; i=0; while(strTwo[i]) { str_Two[i] = strTwo[i]; i++; } str_Two[i] = '\0'; strcat(str_One, str_Two); system(str_One); break; case 2: strOne = "C:\\Windows\\System32\\shutdown /r /t "; i=0; while(strOne[i]) { str_One[i] = strOne[i]; i++; } str_One[i] = '\0'; i=0; while(strTwo[i]) { str_Two[i] = strTwo[i]; i++; } str_Two[i] = '\0'; strcat(str_One, str_Two); system(str_One); break; case 3: return 0; default: cout<<"\nWrong Choice!"; return 0; } return 0; }
Here is the initial output produced by this program:
Now enter your choice, say 2, to restart your computer system. Here is the sample output produced after typing 2 and pressing the ENTER key:
Now enter the number of seconds, say 50, to restart your computer system after 50 seconds.
This C++ program is created for both, i.e., to shutdown (turn off) or restart your computer. First, the program will prompt the user to choose whether to shut down or restart the computer.The first option is given to shut down the computer, whereas the second option is given to restart the computer system.
In C++, shutdown a Linux-based system
This program is created for Linux users. The system() function is also used in Linux. That is, the function is the same, but the command gets changed. Function relates to C++, whereas command relates to the local system software.
#include<stdio.h> int main() { system("shutdown -P now"); return 0; }
Note: For a Linux user, to shutdown a computer, you need to be logged in as root.
Set a timer before shutting down Linux
In Linux, too, you can set a timer in this way:
system("shutdown -P number of minutes");
For example,
system("shutdown -P 5");
will shutdown your system after five minutes of executing the program.
Note: In Windows-based systems, "timer refers to the number of seconds. Whereas in a Linux-based system, "timer" refers to the number of minutes.
Shutdown Windows XP-based systems in C++
This is the end of this article. It is created for Windows XP users.
#include<stdlib.h> int main() { system("C:\\Windows\\System32\\shutdown -s"); return 0; }
That is, instead of /s, use -s to shutdown a Windows XP-based computer system.
The same program in different languages
« Previous Program C++ Tutorial »