#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; } 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(); } } 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; } int 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++; } return score; } 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)]); } } string enter_username() { string username; cout << "Gib deinen Nutzernamen an:\n"; cin >> username; return username; } void write_score_list(const string& filename, const vector>& score_list) { string text; for (const vector& score : score_list) { text += score[0] + ";" + score[1] + "\n"; } ofstream file(filename); file << text; file.close(); } void add_to_scoreboard(const string& username, const int& score) { // --------------------- add to temp vector --------------------- // string filename = "scores.txt"; vector> score_list = get_questions(filename); vector new_score = {username, to_string(score)}; if (score_list.empty()) { cout << "Ignore previous error" << endl; append_file("scores.txt", new_score[0] + ";" + new_score[1]); return; } for (vector& score : score_list) { if (new_score[0] == score[0]) { //if name is the same if (stoi(new_score[1]) > stoi(score[1])) { //and new score is higher score = new_score; write_score_list(filename, score_list); } } else { append_file(filename, new_score[0] + ";" + new_score[1]); } } } int main() { cout << "Willkommen im Knusprigen Quiz" << endl; vector> question_array = get_questions("fragen.txt"); randomize_questions(question_array); int score = ask_questions(question_array); string username = enter_username(); add_to_scoreboard(username, score); //show_leaderboard(); return 0; }