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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;
void question(const std::string& fname)
{
vector<string> lines;
ifstream file(fname);
if (!file) {
std::cout << "Cannot open file " << fname << '\n';
return;
}
for (string line; getline(file, line); lines.push_back(line));
const auto random_number{ rand() % lines.size() };
const auto word1{ lines[random_number] };
const auto codes1{ new int[word1.length()] {} };
for (size_t i = 0; i < word1.length(); ++i)
if (isalpha(static_cast<unsigned char>(word1[i]))) {
codes1[i] = std::tolower(static_cast<unsigned char>(word1[i])) - 'a' + 1;
cout << codes1[i] << ' ';
}
string answer1;
cout << "\nPlease input your answer (as one word no spaces): ";
cin >> answer1;
if (answer1 == word1)
cout << "Correct!\n";
else
cout << "Incorrect :(\n";
}
struct Quiz {
string fname;
string title;
string catnum;
};
int main()
{
const Quiz qz[]{ {"Premier_League_Teams.txt", "Premier League Football Teams!", "first"},
{"Premier_League_Teams.txt", "Premier League Football Teams!", "second"},
{"Hobbit_Characters.txt", "Hobbit Characters", "third"},
{"Hobbit_Characters.txt", "Hobbit Characters", "fourth"},
{"South_American_Countries.txt", "South American Countries", "final"} };
srand(static_cast<unsigned int>(time(nullptr)));
cout << "epic quiz.\n\n\n";
cout << "The objective of this round is to unscramble the coded word.\n";
cout << "You will be given the category as the clue\n";
cout << "and you have to type out what you believe the answer to be, with a capital letter\n\n";
string Name;
cout << "But First, Enter your first name: ";
getline(cin, Name);
cout << "\nWelcome contestant " << Name << ". Are you ready to begin the quiz?\n";
cout << "Please type Yes or No (case sensitive): ";
string respond;
cin >> respond;
if (respond != "Yes") {
cout << "Maybe next time!\n";
return 0;
}
cout << "Good luck!\n";
for (const auto& quiz : qz) {
cout << "The " << quiz.catnum << " category is...\n" << quiz.title << '\n';
question(quiz.fname);
}
}
|