Menu driven program for system control using C++ - GeeksforGeeks

13 Jul, 2020
Prerequisite: Switch-case in C/C++
Problem Statement:Write a menu-driven program to control your system such as shutdown, restart, log off, manual shutdown settings, abort the shutdown, and exit, using Switch-case.
Approach: The idea is to use system() in C. This function is used to invoke operating system commands from the C++ program. In this program to use some system commands listed below:
Shutdown: To shutdown system, use system() function and command shutdown with option s as:system("C:\\WINDOWS\\System32\\shutdown/s")Restart: To restart system, use system() function and command shutdown with option r as:system("C:\\WINDOWS\\System32\\shutdown/r")Log off: To logoff system, use system() function and command shutdown with option l as:system("C:\\WINDOWS\\System32\\shutdown/l")Manual Shutdown: To manual shutdown system, use system() function and command shutdown with option i as:system("C:\\WINDOWS\\System32\\shutdown/i")Abort the Shutdown: To abort the shutdown system, use system() function and command shutdown with option a as:system("C:\\WINDOWS\\System32\\shutdown/a")
Shutdown: To shutdown system, use system() function and command shutdown with option s as:system("C:\\WINDOWS\\System32\\shutdown/s")
system("C:\\WINDOWS\\System32\\shutdown/s")
Restart: To restart system, use system() function and command shutdown with option r as:system("C:\\WINDOWS\\System32\\shutdown/r")
system("C:\\WINDOWS\\System32\\shutdown/r")
Log off: To logoff system, use system() function and command shutdown with option l as:system("C:\\WINDOWS\\System32\\shutdown/l")
system("C:\\WINDOWS\\System32\\shutdown/l")
Manual Shutdown: To manual shutdown system, use system() function and command shutdown with option i as:system("C:\\WINDOWS\\System32\\shutdown/i")
system("C:\\WINDOWS\\System32\\shutdown/i")
Abort the Shutdown: To abort the shutdown system, use system() function and command shutdown with option a as:system("C:\\WINDOWS\\System32\\shutdown/a")
system("C:\\WINDOWS\\System32\\shutdown/a")
Below implementation of the above system function and commands:
CPP
// Menu driven program in CPP to// system control using system()// function and command with option#include <iostream>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <windows.h>  void printMenu();  // Function to shutdown the computervoid shutDown(){    // Tp clears the screen    system("cls");    printf("\nshuttingg down..\n");      // System function call to    // shutdown system    system("C:\\WINDOWS\\System32\\shutdown /s");      // To clears the screen    system("cls");}  // Function to restart the computervoid reStart(){      // Clears the screen    system("cls");    printf("\nRestart in 30 seconds ...");      // System function call to    // restart system    system("C:\\WINDOWS\\system32\\shutdown /r");}  // Function to log off uservoid logOff(){      // To clears the screen    system("cls");    printf("\n Shutting down under 30 seconds... ");      // System function call to log off user    system("C:\\WINDOWS\\system32\\shutdown /l");}  // Function to open manualShutdown// shutdown dialog boxvoid manualShutdown(){    // To clears the screen    system("cls");      // System function call to manual shutdown    system("C:\\WINDOWS\\System32\\shutdown /i");}  void abortShutdown(){    // To clears the screen    system("cls");      // System function call to aboart shutdown    system("C:\\WINDOWS\\System32\\shutdown /a");}  // Function to take user choices and perform// the appropriate operationvoid selectMenu(){    int choice;    printf("\n Enter your choice : ");    scanf("%d", &choice);      switch (choice) {    case 1:        shutDown();        break;      case 2:        reStart();        break;      case 3:        logOff();        break;      case 4:        manualShutdown();        break;      case 5:        abortShutdown();        break;      case 6:          printf("\n Exiting... \n\n");        printf("Exiting in 3 seconds...\n");        Sleep(3000);        exit(1);      default:        printf("\ninvalid choice Try again \n");        printMenu();    }}  // Function to print all the menusvoid printMenu(){    // Set output color to blue    // background and white foreground    system("color 1F");    printf("\n");      // Create Menu    printf("\xB2 \xB2\xB2\xB2\xB2\xB2\xB2\xB2"           "\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"           "\xB2\xB2\xB2\xB2\xB2 SYSTEM CONTROL \xB2"           "\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"           "\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2"           "\xB2\xB2 \xB2");    printf("\n ______________________________"           "_________________________________");    printf("\n|\t\t\t\t\t\t\t\t|");    printf("\n|\t\t\t\t\t\t\t\t|");    printf("\n|\t\t\t 1. Shutdown Computer \t\t\t|");    printf("\n|\t\t\t 2. Restart Computer \t\t\t|");    printf("\n|\t\t\t 3. Log off \t\t\t\t|");    printf("\n|\t\t\t 4. Manual Shutdown Settings\t\t|");    printf("\n|\t\t\t 5. Abort Shutdown \t\t\t|");    printf("\n|\t\t\t 6. Exit \t\t\t\t|");    printf("\n|\t\t\t\t\t\t\t\t|");    printf("\n|\t\t\t\t\t\t\t\t|");    printf("\n|\t\t\t\t\t\t\t\t|");    printf("\n|\t\t\t\t\t\t\t\t|");    printf("\n\xB2_________________________________"           "______________________________\xB2\n");      // Function call for select options    selectMenu();}  // Driver Codeint main(){    // Function Call    printMenu();    return 0;}
C++ Programs
Linux-Unix
Project
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to implement Singly Linked List in C++ using class
Passing a function as a parameter in C++
Const keyword in C++
cout in C++
Difference between user defined function and library function in C/C++
Sed Command in Linux/Unix with examples
AWK command in Unix/Linux with examples
grep command in Unix/Linux
TCP Server-Client implementation in C
cut command in Linux with examples