Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1.     //returns a choice >= 0 if success, -1 if quit
  2.     template<class iter>
  3.     int create_menu(int num_options, iter it, const std::string &menu_name, bool allow_quits = false) {
  4.  
  5.         int selection = 0;
  6.  
  7.         iter start = it;
  8.         while (1) {
  9.             it = start;
  10.             system("CLS");
  11.             printf("%s\n", menu_name.c_str());
  12.             printf("Enter 'w' to move up\n");
  13.             printf("Enter 's' to move down\n");
  14.             printf("Enter 'e' to select\n");
  15.             if (allow_quits) {
  16.                 printf("Enter 'ESC' to quit\n");
  17.             }
  18.             printf("\n");
  19.  
  20.             if (!num_options) {
  21.                 printf("No options available!\n");
  22.                 system("PAUSE");
  23.                 return -1;
  24.             }
  25.  
  26.             for (int a = 0; a < num_options; a++, it++) {
  27.                 if (a == selection) {
  28.                     printf(" > ");
  29.                 } else {
  30.                     printf("   ");
  31.                 }
  32.                 printf("%s\n", it->c_str());
  33.             }
  34.             printf("\n");
  35.  
  36.             char input = tolower(_getch());
  37.  
  38.             if (input == 'w') {
  39.                 selection = std::max(0, selection - 1);
  40.             } else if (input == 's') {
  41.                 selection = std::min(num_options - 1, selection + 1);
  42.             } else if (input == 'e') {
  43.                 return selection;
  44.             } else if (allow_quits && input == KEY::ESC) {
  45.                 return -1;
  46.             }
  47.         }
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement