#include #include #include #include #include #include using namespace std; vector split(string s, const string& delimiter) { vector tokens; size_t pos = 0; while ((pos = s.find(delimiter)) != string::npos) { tokens.push_back(s.substr(0, pos)); s.erase(0, pos + delimiter.length()); } tokens.push_back(s); return tokens; } void string_tolower(string& str) { for (char& chr : str) { chr = tolower(chr); } } string read_file(const string& filename) { ifstream file(filename); if (!file.is_open()) { cerr << "Error: Could not open " << filename << endl; return ""; } string line, content; while (getline(file, line)) content += line + "\n"; return content; } vector> create_question_array(const string& fileText) { vector> question_array; vector lines = split(fileText, "\n"); for (string& line : lines) { if (!line.empty()) question_array.push_back(split(line, ";")); } return question_array; } vector> get_questions(const string& filename) { string fileText = read_file(filename); vector> questionArray = create_question_array(fileText); return questionArray; } void ask_questions(vector>& question_array) { unsigned int score= 0, index = 1; for (vector& question : question_array) { string answer; cout << index << ". "; cout << question[0] << endl << " "; getline(cin, answer); string_tolower(answer); string_tolower(question[1]); if (answer == question[1]) { score++; cout << " Richtig! "; } else { cout << " Falsch! "; } cout << "[" << score << "/" << question_array.size() << "]\n\n"; index++; } } int random_number(int max) { // Define range int min = 0; // Initialize a random number generator random_device rd; mt19937 gen(rd()); uniform_int_distribution<> distrib(min, max); // Generate random number in the range [min, max] int randomValue = distrib(gen); return randomValue; } void randomize_questions(vector>& question_array) { for (vector& question : question_array) { swap(question, question_array[random_number(question_array.size()-1)]); } } int main() { cout << "Willkommen im Knusprigen Quiz" << endl; vector> question_array = get_questions("fragen.txt"); randomize_questions(question_array); ask_questions(question_array); return 0; }