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
|
#include <iostream>
#include <string>
#include <cctype>
#include <stdexcept>
enum difficulty { Easy, Medium, Hard, Expert, Master, Insane };
constexpr int NUM_DIFF = Insane + 1 ;
const std::string difficulty_str[NUM_DIFF] = { "Easy", "Medium", "Hard", "Expert", "Master", "Insane" };
enum difficulty_level { EasyD = 10, MediumD = 100, HardD = 1000, ExpertD = 10000, MasterD = 100000, InsaneD = 1000000 };
difficulty_level get_difficulty()
{
static constexpr difficulty_level level[NUM_DIFF] = { EasyD, MediumD, HardD, ExpertD, MasterD, InsaneD };
static const std::string choices = "ABCDEF" ;
std::cout << "Choose a difficulty!\n" ;
for( int i = 0 ; i < NUM_DIFF ; ++i )
std::cout << choices[i] << ") " << difficulty_str[i] << " - " << level[i] << '\n' ;
char input;
if( std::cin >> input )
{
std::size_t i ;
if( ( i = choices.find( std::toupper(input) ) ) != std::string::npos ) return level[i] ;
std::cout << "\nUnrecognized choice, try again.\n" ;
std::cin.ignore( 1000, '\n' ) ; // empty input buffer (optional)
return get_difficulty() ; // try again
}
else throw std::runtime_error( "eof on stdin" ) ;
}
|