#include #include #include #include #include #include using namespace std; // ============================= UTILS ================================= // 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); } } 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; } // -------------------------- file utils -------------------------------- // 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; } void append_file(const string& filename, const string& text){ ofstream of; of.open(filename, ios::app); if (!of) cout << "No such file found"; else { of << text << endl; of.close(); } } // ============================ LOGIC ============================ // vector> create_question_vector(const string& fileText) { vector> questionVector; vector lines = split(fileText, "\n"); //split on line break for (string& line : lines) { if (!line.empty()) questionVector.push_back(split(line, ";")); //create new vector per line and split on ; } return questionVector; } vector> get_questions(const string& fileName) { string fileText = read_file(fileName); vector> questionVector = create_question_vector(fileText); //create vector out of plain text return questionVector; } int ask_questions(vector>& questionVector) { unsigned int score= 0, index = 1; for (vector& question : questionVector) { string answer; cout << index << ". "; cout << question[0] << endl << " "; // ask question getline(cin, answer); // get answer string_tolower(answer); // make case irellevant string_tolower(question[1]); if (answer == question[1]) { score++; cout << " Richtig! "; } else { cout << " Falsch! "; } cout << "[" << score << "/" << questionVector.size() << "]\n\n"; // display score/highest reachable score index++; // count up in question number } return score; } void randomize_questions(vector>& questionVector) { for (vector& question : questionVector) { // traverse vector and swap element with random index swap(question, questionVector[random_number(questionVector.size()-1)]); } } string enter_username() { string username; cout << "Gib deinen Nutzernamen an:\n"; cin >> username; return username; } void write_score_list(const string& filename, const vector>& scoreList) { string text; for (const vector& score : scoreList) { text += score[0] + ";" + score[1] + "\n"; } ofstream file(filename); file << text; file.close(); } void add_to_scoreboard(const string& username, const int& score) { // --------------------- read --------------------- // string filename = "scores.txt"; vector> scoreList = get_questions(filename); // read existing scores from file vector newScore = {username, to_string(score)}; // create new score from this run // if no file existed yet/it's empty if (scoreList.empty()) { cout << "Ignore previous error" << endl; append_file("scores.txt", newScore[0] + ";" + newScore[1]); return; } //if file already existed for (vector& score : scoreList) { if (newScore[0] == score[0]) { //if name already exists if (stoi(newScore[1]) > stoi(score[1])) { //and new score is higher score = newScore; write_score_list(filename, scoreList); } } else { // if name is new append_file(filename, newScore[0] + ";" + newScore[1]); } } } int main() { cout << "Willkommen im Knusprigen Quiz" << endl; vector> questionVector = get_questions("fragen.txt"); //load questions into vector randomize_questions(questionVector); int score = ask_questions(questionVector); string username = enter_username(); add_to_scoreboard(username, score); //show_leaderboard(); return 0; }