pa/2025-10-17: improve comments and uniform case
crispy-caesus crispy@crispy-caesus.eu
Wed, 22 Oct 2025 13:08:15 +0200
1 files changed,
60 insertions(+),
51 deletions(-)
M
discord_programmieraufgaben/2025-10-17_2/quiz.cpp
→
discord_programmieraufgaben/2025-10-17_2/quiz.cpp
@@ -7,6 +7,8 @@ #include <random>
using namespace std; +// ============================= UTILS ================================= // + vector<string> split(string s, const string& delimiter) { vector<string> tokens; size_t pos = 0;@@ -24,6 +26,24 @@ 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()) {@@ -47,32 +67,34 @@ of.close();
} } -vector<vector<string>> create_question_array(const string& fileText) { - vector<vector<string>> question_array; - vector<string> lines = split(fileText, "\n"); +// ============================ LOGIC ============================ // + +vector<vector<string>> create_question_vector(const string& fileText) { + vector<vector<string>> questionVector; + vector<string> lines = split(fileText, "\n"); //split on line break for (string& line : lines) { - if (!line.empty()) question_array.push_back(split(line, ";")); + if (!line.empty()) questionVector.push_back(split(line, ";")); //create new vector per line and split on ; } - return question_array; + return questionVector; } -vector<vector<string>> get_questions(const string& filename) { - string fileText = read_file(filename); - vector<vector<string>> questionArray = create_question_array(fileText); +vector<vector<string>> get_questions(const string& fileName) { + string fileText = read_file(fileName); + vector<vector<string>> questionVector = create_question_vector(fileText); //create vector out of plain text - return questionArray; + return questionVector; } -int ask_questions(vector<vector<string>>& question_array) { +int ask_questions(vector<vector<string>>& questionVector) { unsigned int score= 0, index = 1; - for (vector<string>& question : question_array) { + for (vector<string>& question : questionVector) { string answer; cout << index << ". "; - cout << question[0] << endl << " "; - getline(cin, answer); + cout << question[0] << endl << " "; // ask question + getline(cin, answer); // get answer - string_tolower(answer); + string_tolower(answer); // make case irellevant string_tolower(question[1]); if (answer == question[1]) { score++;@@ -80,33 +102,18 @@ cout << " Richtig! ";
} else { cout << " Falsch! "; } - cout << "[" << score << "/" << question_array.size() << "]\n\n"; + cout << "[" << score << "/" << questionVector.size() << "]\n\n"; // display score/highest reachable score - index++; + index++; // count up in question number } 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<vector<string>>& question_array) { - for (vector<string>& question : question_array) { - swap(question, question_array[random_number(question_array.size()-1)]); +void randomize_questions(vector<vector<string>>& questionVector) { + for (vector<string>& question : questionVector) { // traverse vector and swap element with random index + swap(question, questionVector[random_number(questionVector.size()-1)]); } }@@ -118,9 +125,9 @@
return username; } -void write_score_list(const string& filename, const vector<vector<string>>& score_list) { +void write_score_list(const string& filename, const vector<vector<string>>& scoreList) { string text; - for (const vector<string>& score : score_list) { + for (const vector<string>& score : scoreList) { text += score[0] + ";" + score[1] + "\n"; }@@ -130,25 +137,27 @@ file.close();
} void add_to_scoreboard(const string& username, const int& score) { - // --------------------- add to temp vector --------------------- // + // --------------------- read --------------------- // string filename = "scores.txt"; - vector<vector<string>> score_list = get_questions(filename); - vector<string> new_score = {username, to_string(score)}; + vector<vector<string>> scoreList = get_questions(filename); // read existing scores from file + vector<string> newScore = {username, to_string(score)}; // create new score from this run - if (score_list.empty()) { + // if no file existed yet/it's empty + if (scoreList.empty()) { cout << "Ignore previous error" << endl; - append_file("scores.txt", new_score[0] + ";" + new_score[1]); + append_file("scores.txt", newScore[0] + ";" + newScore[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); + //if file already existed + for (vector<string>& 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 { - append_file(filename, new_score[0] + ";" + new_score[1]); + } else { // if name is new + append_file(filename, newScore[0] + ";" + newScore[1]); } }@@ -157,9 +166,9 @@
int main() { cout << "Willkommen im Knusprigen Quiz" << endl; - vector<vector<string>> question_array = get_questions("fragen.txt"); - randomize_questions(question_array); - int score = ask_questions(question_array); + 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();