1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <string>
#include <cctype>
std::string get_choice( std::string choice_1, std::string choice_2, char option_1 = '1', char option_2 = '2' )
{
std::cout << "please enter either " << option_1 << " for '" << choice_1 << "' or "
<< option_2 << " for '" << choice_2 << "': " ;
char choice ;
std::cin >> choice ;
choice = std::tolower(choice) ;
if( choice == option_1 ) return choice_1 ;
else if( choice == option_2 ) return choice_2 ;
else return get_choice( choice_1, choice_2, option_1, option_2 ) ; // invalid input; try again
}
void menu( std::string& gender, std::string& strength_or_skill, std::string& size_or_speed, std::string& health_or_stamina )
{
std::cout << "\n1) gender?\n" ;
gender = get_choice( "male", "female", 'm', 'f' ) ;
std::cout << "\n2) strength or skill?\n" ;
strength_or_skill = get_choice( "strength", "skill" ) ;
std::cout << "\n3) size or speed?\n" ;
size_or_speed = get_choice( "size", "speed" ) ;
std::cout << "\n4) health or stamina?\n" ;
health_or_stamina = get_choice( "health", "stamina", 'h', 's' ) ;
}
bool correct( std::string gender, std::string strength_or_skill, std::string size_or_speed, std::string health_or_stamina )
{
std::cout << "\nYou've chosen '" << gender << "', '" << strength_or_skill << "', '"
<< size_or_speed << "' and '" << health_or_stamina << "'\nare these correct?\n" ;
return get_choice( "yes", "no", 'y', 'n' ) == "yes" ;
}
int main()
{
std::string gender, strength_or_skill, size_or_speed, health_or_stamina ;
do menu( gender, strength_or_skill, size_or_speed, health_or_stamina ) ;
while( !correct( gender, strength_or_skill, size_or_speed, health_or_stamina ) ) ;
// ...
}
|