fork download
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. //Forward Declarations
  5. void menu();
  6. void select_M();
  7. void select_A();
  8. void select_C();
  9. void select_N();
  10. void select_E();
  11. char isValid(char &);
  12. void selection_error();
  13.  
  14. //MAIN
  15. int main(){
  16.  
  17. menu();
  18.  
  19. return 0;
  20. }
  21.  
  22. //MAIN MENU
  23. void menu(){
  24. char selection{}; //declares and initiallizes selection to null
  25.  
  26. std::cout << "(M) Selection 1" << std::endl;
  27. std::cout << "(A) Selection 2" << std::endl;
  28. std::cout << "(C) Selection 3" << std::endl;
  29. std::cout << "(N) Selection 4" << std::endl;
  30. std::cout << "(E) Selection 5" << std::endl;
  31. std::cout << "(Q) Quit" << std::endl;
  32. std::cout << std::endl;
  33. std::cout << "Enter your selection: ";
  34. isValid(selection);
  35.  
  36. switch(selection){
  37. case 'M': select_M(); break;
  38. case 'A': select_A(); break;
  39. case 'C': select_C(); break;
  40. case 'N': select_N(); break;
  41. case 'E': select_E(); break;
  42. case 'Q': return; //break;
  43. default: selection_error(); break;
  44.  
  45. }//switch
  46. system("cls"); //used for testing - not recommended for use in actual program
  47. menu();
  48. }//menu
  49.  
  50. //INPUT VALIDATION
  51. char isValid(char &selection){
  52.  
  53. std::cin >> selection;
  54. selection = toupper(selection); // ctype.h for toupper changes all to uppercase characters
  55.  
  56. //checks to see if more than 1 character is inputed
  57. if (std::cin.get() != '\n'){
  58. std::cin.ignore(256, '\n'); //ignores 256 chars until newline('\n')
  59. std::cin.clear(); // clears the input
  60. selection = '\0'; // sets selection to null
  61. system("cls");//used for testing - not recommended for use in actual program
  62. }
  63. return selection;
  64. }
  65.  
  66.  
  67. void selection_error(){
  68. std::cout << "Invaild Selection"<< std::endl;
  69. system("pause"); //used for testing - not recommended for use in actual program
  70. }
  71.  
  72. void select_M(){
  73. std::cout << "M was Selected"<< std::endl;
  74. system("pause"); //used for testing - not recommended for use in actual program
  75. }
  76.  
  77. void select_A(){
  78. std::cout << "A was Selected"<< std::endl;
  79. system("pause"); //used for testing - not recommended for use in actual program
  80. }
  81.  
  82. void select_C(){
  83. std::cout << "C was Selected"<< std::endl;
  84. system("pause"); //used for testing - not recommended for use in actual program
  85. }
  86.  
  87. void select_N(){
  88. std::cout << "N was Selected"<< std::endl;
  89. system("pause"); //used for testing - not recommended for use in actual program
  90. }
  91.  
  92. void select_E(){
  93. std::cout << "E was Selected"<< std::endl;
  94. system("pause"); //used for testing - not recommended for use in actual program
  95. }
Success #stdin #stdout #stderr 0s 15240KB
stdin
m
a
c
n
e
aa
as
k
l
q
stdout
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: M was Selected
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: A was Selected
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: C was Selected
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: N was Selected
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: E was Selected
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: Invaild Selection
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: Invaild Selection
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: Invaild Selection
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: Invaild Selection
(M) Selection 1
(A) Selection 2
(C) Selection 3
(N) Selection 4
(E) Selection 5
(Q) Quit

Enter your selection: 
stderr
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found
sh: 1: pause: not found
sh: 1: cls: not found