pa/2025-10-17: l6(no gui) add scoreboard
crispy-caesus crispy@crispy-caesus.eu
Wed, 22 Oct 2025 13:36:05 +0200
1 files changed,
22 insertions(+),
7 deletions(-)
M
discord_programmieraufgaben/2025-10-17_2/quiz.cpp
→
discord_programmieraufgaben/2025-10-17_2/quiz.cpp
@@ -136,7 +136,7 @@ file << text;
file.close(); } -void add_to_scoreboard(const string& username, const int& score) { +vector<vector<string>> add_to_scoreboard(const string& username, const int& score) { // --------------------- read --------------------- // string filename = "scores.txt"; vector<vector<string>> scoreList = get_questions(filename); // read existing scores from file@@ -145,8 +145,9 @@
// if no file existed yet/it's empty if (scoreList.empty()) { cout << "Ignore previous error" << endl; + scoreList.push_back(newScore); append_file("scores.txt", newScore[0] + ";" + newScore[1]); - return; + return scoreList; } //if file already existed@@ -155,12 +156,26 @@ 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); + return scoreList; } - } else { // if name is new - append_file(filename, newScore[0] + ";" + newScore[1]); - } + } } + + scoreList.push_back(newScore); + append_file(filename, newScore[0] + ";" + newScore[1]); + return scoreList; +} + +void show_leaderboard(const vector<vector<string>>& scoreList) { + if (scoreList.empty()) { + cout << "wtF"; + } + cout << "#==================================#" << endl; + for (vector<string> score : scoreList) { + cout << score[0] << ": " << score[1] << endl; + } + cout << "#==================================#"; } int main() {@@ -170,8 +185,8 @@ vector<vector<string>> 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(); + vector<vector<string>> newScoreList = add_to_scoreboard(username, score); + show_leaderboard(newScoreList); return 0; }