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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int question2();
int askForSecondChance(const std::string& question, int attempts = 1);
void waitForEnter();
int main()
{
question2();
waitForEnter();
return 0;
}
int question2()
{
std::vector<std::string> q2words { "people", "powerlines", "slips and spills",
"snakes", "trips and falls", "vermin",
"trees" };
std::cout << "\nQ2: Name 7 hazards: \n";
int result {static_cast<int>(q2words.size())}, index {};
for(const auto& a : q2words) {
std::cout << "hazard n. " << ++index << ": ";
std::string tmp;
std::getline(std::cin, tmp);
if(a != tmp) {
int chances = askForSecondChance(a);
if(chances < static_cast<int>(a.length())-1) { result--; }
} else {
std::cout << "Very good!\n";
result--;
}
}
std::cout << "You missed " << result << " answers out of "
<< q2words.size() << '\n';
return result;
}
int askForSecondChance(const std::string& question, int attempts)
{
if(attempts < static_cast<int>(question.length())-1) {
std::cout << "Ouch! Sorry, wrong answer. Please, try again ("
<< question.substr(0, attempts) << "...): ";
std::string tmp;
std::getline(std::cin, tmp);
if(tmp != question) {
attempts = askForSecondChance(question, ++attempts);
}
} else {
std::cout << "Sorry, you've run out of chances.\n";
}
return attempts;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
|