pa/2025-10-17: pl6 score backend no gui cause lazy no way(besides reading the file manually) to check scores yet
crispy-caesus crispy@crispy-caesus.eu
Wed, 22 Oct 2025 01:42:36 +0200
1 files changed,
65 insertions(+),
2 deletions(-)
M
discord_programmieraufgaben/2025-10-17_2/quiz.cpp
→
discord_programmieraufgaben/2025-10-17_2/quiz.cpp
@@ -35,6 +35,18 @@ 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<vector<string>> create_question_array(const string& fileText) { vector<vector<string>> question_array; vector<string> lines = split(fileText, "\n");@@ -51,7 +63,7 @@
return questionArray; } -void ask_questions(vector<vector<string>>& question_array) { +int ask_questions(vector<vector<string>>& question_array) { unsigned int score= 0, index = 1; for (vector<string>& question : question_array) {@@ -72,6 +84,8 @@ cout << "[" << score << "/" << question_array.size() << "]\n\n";
index++; } + + return score; } int random_number(int max) {@@ -96,10 +110,59 @@ 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<vector<string>>& score_list) { + string text; + for (const vector<string>& 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<vector<string>> score_list = get_questions(filename); + vector<string> 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<string>& 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<vector<string>> question_array = get_questions("fragen.txt"); randomize_questions(question_array); - ask_questions(question_array); + int score = ask_questions(question_array); + string username = enter_username(); + add_to_scoreboard(username, score); + //show_leaderboard(); + return 0; }