Advertisement
etonw

function: take y/ n char input: easily modified for other char input

Jun 28th, 2022
1,480
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None
  1. char input_char_switch_y_n()
  2. {
  3.     char char_input = ' ';
  4.     do
  5.     {
  6.         cout << endl << "\t\tRepeat program?  Y (Yes) /  N (No) " ;
  7.         cin >> char_input;
  8.         switch(char_input)
  9.         {
  10.             case 'Y':
  11.             case 'y':  // allow for both upper and lower case letters
  12.                 char_input = 'Y';
  13.                 break;
  14.            
  15.             case 'N':
  16.             case 'n':
  17.                 char_input = 'N';
  18.                 break;
  19.  
  20.          
  21.             default:   //  no legal option was entered     
  22.                 cout << endl
  23.                      << " Not a valid choice. " << endl
  24.                      << " Choose again." << endl << endl;
  25.         }
  26.     } while(char_input != 'Y' && char_input != 'N' );  // loop until correct input entered
  27.     return char_input;
  28. } // end function
Advertisement
RAW Paste Data Copied
Advertisement