The code example allows the user to choose between shutting down or restarting their computer. The program prompts the user for input and uses a switch
statement to execute the corresponding system command based on the user’s choice.
#include <iostream>
#include <cstdlib> // Include the cstdlib header for the system() function
using namespace std;
// Function prototypes
void shutdownComputer();
void restartComputer();
void handleInvalidInput();
int main() {
int n;
// Prompt the user for input
cout << "Press 1 to shut down the computer or 2 to restart it: ";
cin >> n;
// Handle user input using a switch statement
switch (n) {
case 1:
shutdownComputer();
break;
case 2:
restartComputer();
break;
default:
handleInvalidInput();
break;
}
return 0;
}
// Function to shut down the computer
void shutdownComputer() {
cout << "System shutdown after 10 seconds." << endl;
system("c:\\windows\\system32\\shutdown /s /t 10 \n");
}
// Function to restart the computer
void restartComputer() {
cout << "System restart after 10 seconds." << endl;
system("c:\\windows\\system32\\shutdown /r /t 10 \n");
}
// Function to handle invalid input
void handleInvalidInput() {
cout << "Incorrect input. Please enter 1 to shut down or 2 to restart the computer." << endl;
}
void shutdownComputer();
void restartComputer();
void handleInvalidInput();
shutdownComputer()
, restartComputer()
, and handleInvalidInput()
before their actual definitions.int main() {
int n;
// Prompt the user for input
cout << "Press 1 to shut down the computer or 2 to restart it: ";
cin >> n;
// Handle user input using a switch statement
switch (n) {
case 1:
shutdownComputer();
break;
case 2:
restartComputer();
break;
default:
handleInvalidInput();
break;
}
return 0;
}
int main()
: The main function where the execution of the program begins.int n;
: Declares an integer variable n
to store the user’s input.cout << "Press 1 to shut down the computer or 2 to restart it: ";
: Prompts the user to enter 1 or 2.cin >> n;
: Reads the user’s input and stores it in variable n
.The switch
statement evaluates the value of n
:
case 1
: If the user enters 1, the shutdownComputer()
function is called.case 2
: If the user enters 2, the restartComputer()
function is called.default
: If the user enters any other value, the handleInvalidInput()
function is called.void shutdownComputer() {
cout << "System shutdown after 10 seconds." << endl;
system("c:\\windows\\system32\\shutdown /s /t 10 \n");
}
void shutdownComputer()
: Defines a function that prints a message indicating that the system will shut down in 10 seconds.cout << "System shutdown after 10 seconds." << endl;
: Outputs the shutdown message.system("c:\\windows\\system32\\shutdown /s /t 10 \n");
: Executes the system command to shut down the computer after 10 seconds.void restartComputer() {
cout << "System restart after 10 seconds." << endl;
system("c:\\windows\\system32\\shutdown /r /t 10 \n");
}
void restartComputer()
: Defines a function that prints a message indicating that the system will restart in 10 seconds.cout << "System restart after 10 seconds." << endl;
: Outputs the restart message.system("c:\\windows\\system32\\shutdown /r /t 10 \n");
: Executes the system command to restart the computer after 10 seconds.void handleInvalidInput() {
cout << "Incorrect input. Please enter 1 to shut down or 2 to restart the computer." << endl;
}
void handleInvalidInput()
: Defines a function that prints a message indicating that the input is incorrect.cout << "Incorrect input. Please enter 1 to shut down or 2 to restart the computer." << endl;
: Outputs the error message prompting the user to enter a valid option.Let’s consider the different scenarios based on user input:
User inputs 1:
Press 1 to shut down the computer or 2 to restart it: 1
System shutdown after 10 seconds.
User inputs 2:
Press 1 to shut down the computer or 2 to restart it: 2
System restart after 10 seconds.
User inputs an invalid number (e.g., 3):
Press 1 to shut down the computer or 2 to restart it: 3
Incorrect input. Please enter 1 to shut down or 2 to restart the computer.