### Problems in Show the player(s) who got the min and max average Hello. This is a sequel to the question that I posted recently. I'm working on the system and I have trouble showing the player(s) who got the max average and minimum average that I input based on the inputs that I've done in 1. Add record and 3. Compute for the average. I'm stuck here for hours analyzing the codes to find a way to solve this problem. I need help in fixing the code below. Any help will do. Thanks.

In 6. Open the file and 7. Close the file, I'll find a way first in solving this and I'll call for help if I'm stuck in this project.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208`` ``````#include #include #include #include #include using namespace std; const int maxrow = 5; string gb_nickname [maxrow] = {}; double gb_age [maxrow] = {}; double gb_score1 [maxrow] = {}; double gb_score2 [maxrow] = {}; struct variables { string nickname; double age; double score1; double score2; }; void addrecord() { variables v; system ("clear"); cin.ignore (); cout << "Enter nickname: "; getline (cin, v.nickname); cout << "Age: "; cin >> v.age; cout << "Enter Score 1: "; cin >> v.score1; cout << "Enter Score 2: "; cin >> v.score2; for (int x = 0; x < maxrow; x++) if (gb_nickname[x].empty()) { gb_nickname[x] = v.nickname; gb_age[x] = v.age; gb_score1[x] = v.score1; gb_score2[x] = v.score2; break; } ofstream myfile ("players.txt", ios::out | ios::app); myfile << endl; myfile << " Nickname: " << v.nickname << endl << " Age: " << v.age << endl<< " Score 1: " << v.score1 << endl << " Score 2: " << v.score2 << endl; myfile.close (); cout << endl; cout << "File was saved successfully! Press any key to continue... " << endl; cin.get(); } void viewrecord () { char menu; //(y/n) int counter = 0; while (menu != 'M')/* user can input "y" or "Y"*/ { system ("clear"); //clear the screen cout << "\n"; cout << "\t\t\t\t\t\t\b\b\b\b\b\b\b\b RECORDS LIST" << endl; for (int x = 0; x > menu; if (counter == 0) { cout << " No records found." << endl; } } } void average(string search) { char menu; int counter = 0; while (menu != 'M') { for (int x = 0; x < maxrow; x++) { if (gb_nickname[x] == search) { counter++; cout << endl << counter <<"." << " Nickname: " << gb_nickname [x] << " " << endl << " Age: " << gb_age [x] << " "<< endl << " Score 1: " << gb_score1 [x] << " "<< endl << " Score 2: "<< gb_score2 [x] << endl; double sum = gb_score1 [x] + gb_score2 [x]; cout << "The average of " << gb_nickname [x] << " is " <> menu; } } int main() { int num; string gb_nickname; home: do { system ("clear"); cout << " ==THE ARCADE'S BEST==" << endl; cout << "1. Add record" << endl; cout << "2. View players records" << endl; cout << "3. Compute for the average" << endl; cout << "4. Show the player(s) who got the max average" << endl; cout << "5. Show the player(s) who gets the min average" << endl; cout << "6. Open the file" << endl; cout << "7. Close the file" << endl; cout << "8. Exit" << endl; cout << "\nPick a number: "; cin >> num; switch (num) { case 1: addrecord(); break; case 2: viewrecord(); break; case 3: system ("clear"); cin.ignore(); cout << "Search Nickname: "; getline (cin, gb_nickname); average(gb_nickname); break; } } while (num != 8); return 0; } ``````

Last edited on As a possible refactor, consider:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192`` ``````#include #include #include #include #include constexpr size_t maxrow {5}; struct variables { std::string nickname; double age {}; double score1 {}; double score2 {}; double avg {}; }; // Global variables are a BAD IDEA variables gb_var[maxrow] {}; double max_avg {std::numeric_limits::min()}, min_avg {std::numeric_limits::max()}; std::istream& operator>>(std::istream& is, variables& v) { return std::getline(is >> std::ws, v.nickname, ',') >> v.age >> v.score1 >> v.score2; } bool addrecord(variables& v) { size_t pos {}; bool found {}; for (size_t x {}; x < maxrow && !found; ++x) { found = gb_var[x].nickname == v.nickname; if (!found && pos == 0 && gb_var[x].nickname.empty()) pos = x + 1; } if (found) { std::cout << "Name already present\n"; return false; } if (pos == 0) { std::cout << "Array full\n"; return false; } v.avg = (v.score1 + v.score2) / 2.0; if (v.avg > max_avg) max_avg = v.avg; if (v.avg < min_avg) min_avg = v.avg; gb_var[pos - 1] = v; return true; } bool getrecord() { variables v; std::cout << "Enter nickname: "; std::getline(std::cin, v.nickname); std::cout << "Age: "; std::cin >> v.age; std::cout << "Enter Score 1: "; std::cin >> v.score1; std::cout << "Enter Score 2: "; std::cin >> v.score2; return addrecord(v); } void viewrecord() { size_t counter {}; std::cout << "\n\t\t\t\t\t\t\b\b\b\b\b\b\b\b RECORDS LIST\n"; for (size_t x {}; x < maxrow; ++x) if (!gb_var[x].nickname.empty()) std::cout << '\n' << ++counter << "." << " Nickname: " << gb_var[x].nickname << '\n' << " Age: " << gb_var[x].age << '\n' << " Score 1: " << gb_var[x].score1 << '\n' << " Score 2: " << gb_var[x].score2 << '\n' << " Average: " << gb_var[x].avg << '\n'; if (counter == 0) std::cout << " No records found.\n"; } void average(const std::string& search) { bool found {}; for (size_t x {}; x < maxrow; ++x) if (gb_var[x].nickname == search) { std::cout << "The average for " << gb_var[x].nickname << " is " << gb_var[x].avg << '\n'; found = true; break; } if (!found) std::cout << "No records found.\n"; } int main() { std::ifstream ifs("players.txt"); if (!ifs) std::cout << "Cannot open file\n"; else { for (variables v; ifs >> v; addrecord(v)); std::cout << "Records loaded\n"; ifs.close(); } unsigned num {}; do { std::cout << "\n ==THE ARCADE'S BEST==\n"; std::cout << "1. Add record\n"; std::cout << "2. View players records\n"; std::cout << "3. Display a player average\n"; std::cout << "4. Show the player(s) who got the max average\n"; std::cout << "5. Show the player(s) who gets the min average\n"; std::cout << "6. Exit\n"; std::cout << "\nPick a number: "; std::cin >> num; std::cin.ignore(); switch (num) { case 1: if (getrecord()) std::cout << "Record added OK\n"; break; case 2: viewrecord(); break; case 3: { std::string gb_nickname; std::cout << "Search Nickname: "; std::getline(std::cin, gb_nickname); average(gb_nickname); } break; case 4: std::cout << "Players with maximum average\n"; for (size_t x {}; x < maxrow; ++x) if (gb_var[x].avg == max_avg) std::cout << gb_var[x].nickname << '\n'; break; case 5: std::cout << "Players with minimum average\n"; for (size_t x {}; x < maxrow; ++x) if (gb_var[x].avg == min_avg) std::cout << gb_var[x].nickname << '\n'; break; case 6: { std::ofstream ofs("Players.txt"); if (!ofs) std::cout << "Cannot open file\n"; else { for (size_t x {}; x < maxrow; ++x) if (!gb_var[x].nickname.empty()) ofs << gb_var[x].nickname << ',' << gb_var[x].age << ' ' << gb_var[x].score1 << ' ' << gb_var[x].score2 << '\n'; std::cout << "Data written ok\n"; } } break; default: std::cout << "Invalid option\n"; break; } } while (num != 6); }`````` Thank you so much @seeplus! The program you fixed for me is working. The codes are so advanced and the codes that you've done to it are not available in our modules. That's why I'm out of logic. I'll study the codes and I'll also find a way to do 6. Open the file and 7. Close the file. I'll go to the forum next time when I still got no more logic left to do. I'm so impressed.
Last edited on With the way I've re-factored it, you don't use option 6 (file open) and 7 (file close). The file is opened, read and closed before the menu is displayed. Hence option 2 when first used will show the details read from the file The file is re-written when the menu is exited with option 6 (exit).
Topic archived. No new replies allowed.