Is there an easier way to check the menu options without using the "option != "a" && "b" && ...."c".? Can I just check if character in string and simplify the if and while loops?
Code:char getMenuOption () { char option; option = NULL; // Initialize variable to null do { clrScr (); cout << endl; cout << " *********************************************************\n"; cout << " *\t B A G M E N U \t\t *\n"; cout << " *********************************************************\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " * [P]ut an item in the bag\t\t\t\t *\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " * [G]rab an unknown item from the bag\t\t *\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " * [D]ump the bag\t\t\t\t\t *\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " * [F]ind an item in the bag\t\t\t\t *\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " * Ge[T] a specific item from the bag\t\t\t *\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " * [W]hat's in the bag (verify)\t\t\t *\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " * [Q]uit\t\t\t\t\t\t *\n"; cout << " *\t\t\t\t\t\t\t *\n"; cout << " *********************************************************\n"; // check for null so that error does not appear before input if (option != NULL) { if (option != 'P' && option != 'G' && option != 'D' && option != 'F' && option != 'T' && option != 'W' && option != 'Q') cout << "\a\n *** ATTENTION *** Use Selections:"; cout << " P, G, D, F, T, W, or Q!\n"; } // end if option not null cout << "\n Enter: (P, G, D, F, T, W, or Q) => "; cin >> option; option = toupper (option); // Change to upper case } // end do while option while (option != 'P' && option != 'G' && option != 'D' && option != 'F' && option != 'T' && option != 'W' && option != 'Q'); return option; } // end getMenuOption



LinkBack URL
About LinkBacks


