University of Leeds

Creating menus

Introduction

Command line interfaces (CLI) are very simple by their nature and lack the fancy graphics found in graphical user interfaces (GUI). However, their appearance and ease of user interaction can be improved somewhat by making simple menus.

Example menu system

In this example system, the user is presented with 5 options, selected by entering the relevant integer value. The input is validated to ensure correct operation. The first 4 options call functions in which different applications could be implemented. The fifth option exits the program. In each of the 4 sub-functions, the user can enter 'b' or 'B' to go back to the main menu. Note the use of hyphens (-) and pipes (|) to draw a very primitive box around the menu using the print_main_menu function.

The main function just calls the main_menu function. This prints the main menu, gets the user input and selects the relevant option. Since each sub-function recursively calls this main menu function, it effectively forms a loop. The user can break the loop and exit the program by inputting the exit command.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <regex>

void main_menu();
int get_user_input();
void select_menu_item(int input);
void print_main_menu();
void go_back_to_main_menu();
bool is_integer(std::string num);
void menu_item_1();
void menu_item_2();
void menu_item_3();
void menu_item_4();

int main(int argc, char const *argv[]) {
  main_menu();
  return 0;
}

void main_menu() {
  print_main_menu();
  int input = get_user_input();
  select_menu_item(input);
}

int get_user_input() {
  int input;
  std::string input_string;
  bool valid_input = false;
  int menu_items = 5;

  do {
    std::cout << "\nSelect item: ";
    std::cin >> input_string;
    valid_input = is_integer(input_string);
    // if input is not an integer, print an error message
    if (valid_input == false) {
      std::cout << "Enter an integer!\n";
    } else {  // if it is an int, check whether in range
      input = std::stoi(input_string);  // convert to int
      if (input >= 1 && input <= menu_items) {
        valid_input = true;
      } else {
        std::cout << "Invalid menu item!\n";
        valid_input = false;
      }
    }
  } while (valid_input == false);

  return input;
}

void select_menu_item(int input) {
  switch (input) {
    case 1:
      menu_item_1();
      break;
    case 2:
      menu_item_2();
      break;
    case 3:
      menu_item_3();
      break;
    case 4:
      menu_item_4();
      break;
    default:
      exit(1);
      break;
  }
}

void print_main_menu() {
  std::cout << "\n----------- Main menu -----------\n";
  std::cout << "|\t\t\t\t|\n";
  std::cout << "|\t1. Menu item 1\t\t|\n";
  std::cout << "|\t2. Menu item 2\t\t|\n";
  std::cout << "|\t3. Menu item 3\t\t|\n";
  std::cout << "|\t4. Menu item 4\t\t|\n";
  std::cout << "|\t5. Exit\t\t\t|\n";
  std::cout << "|\t\t\t\t|\n";
  std::cout << "---------------------------------\n";
}

void go_back_to_main() {
  std::string input;
  do {
    std::cout << "\nEnter 'b' or 'B' to go back to main menu: ";
    std::cin >> input;
  } while (input != "b" && input != "B");
  main_menu();
}

// https://codereview.stackexchange.com/questions/162569/checking-if-each-char-in-a-string-is-a-decimal-digit
bool is_integer(std::string num) {
  return std::regex_match(num, std::regex("[+-]?[0-9]+"));
}

void menu_item_1() {
  std::cout << "\n>> Menu 1\n";
  std::cout << "\nSome code here does something useful\n";
  go_back_to_main();
}
void menu_item_2() {
  std::cout << "\n>> Menu 2\n";
  std::cout << "\nSome code here does something useful\n";
  go_back_to_main();
}
void menu_item_3() {
  std::cout << "\n>> Menu 3\n";
  std::cout << "\nSome code here does something useful\n";
  go_back_to_main();
}
void menu_item_4() {
  std::cout << "\n>> Menu 4\n";
  std::cout << "\nSome code here does something useful\n";
  go_back_to_main();
}

Program output

The output of the program is shown below.

----------- Main menu -----------
|                               |
|       1. Menu item 1          |
|       2. Menu item 2          |
|       3. Menu item 3          |
|       4. Menu item 4          |
|       5. Exit                 |
|                               |
---------------------------------

Select item: 1

>> Menu 1

Some code here does something useful

Enter 'b' or 'B' to go back to main menu: