Skip to content
Related Articles

Related Articles

Improve Article
Menu driven program for system control using C++
  • Difficulty Level : Medium
  • Last Updated : 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:

  1. Shutdown: To shutdown system, use system() function and command shutdown with option s as:
    system("C:\\WINDOWS\\System32\\shutdown/s")
  2. Restart: To restart system, use system() function and command shutdown with option r as:
    system("C:\\WINDOWS\\System32\\shutdown/r")
  3. Log off: To logoff system, use system() function and command shutdown with option l as:
    system("C:\\WINDOWS\\System32\\shutdown/l")
  4. Manual Shutdown: To manual shutdown system, use system() function and command shutdown with option i as:
    system("C:\\WINDOWS\\System32\\shutdown/i")
  5. Abort the Shutdown: To abort the shutdown system, use system() function and command shutdown with option a as:
    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 computer
void 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 computer
void 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 user
void 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 box
void 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 operation
void 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 menus
void 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 Code
int main()
{
    // Function Call
    printMenu();
    return 0;
}


Output:
Below is the Output of the above program:
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :