/* testfunc14.cpp CIS 150 6/9/2005 David Klick This program demonstrates a useful data validation function for use with menus. */ #include #include using std::cin; using std::cout; char getChoice(char* prompt, char* allowed); void displayChoice(char* msg); int main(void) { char choice; do { // display menu cout << "Menu\n\n" " (F)irst\n\n" " (S)econd\n\n" " (E)nd\n\n"; // get user choice choice = getChoice("Enter choice: ", "FfSsEe"); choice = toupper(choice); // convert to upper case // process choice switch (choice) { case 'F': displayChoice("the first choice"); break; case 'S': displayChoice("the second option"); break; } } while (choice != 'E'); cout << "Normal program terminiation\n\n"; return 0; } char getChoice(char *prompt, char *allowed) { char ch; char *ptr; do { cout << prompt; cin >> ch; ptr = strchr(allowed, (int)ch); if (ptr == NULL) { cout << "Error: Invalid choice\n"; } } while (ptr == NULL); return ch; } void displayChoice(char* msg) { cout << "You chose " << msg << '\n'; }