### How to compute & compare average of numbers stored in struct

Good day, I am having a hard time trying to make this certain segment of my program work, it should compute for the average of the 2 scores of the players and only print out the players who got the highest average or lowest average among all of the players.

if anyone could provide an example and explanation as to how to do it is much appreciated

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163`` `````` #include #include #include using namespace std; struct playerdata{ char name [50]; }; struct playerdata2{ int age, score1, score2; }; int main(){ // int choice, i=1, j=1, z=1; char backtomain, backtomain2; playerdata p1[i]; playerdata2 p2[j]; main: for(int a = 0; a<47; a++){ cout << "="; } cout << "\n"; for(int b = 0; b<22; b++){ cout << " "; if (b == 21){ cout << "MENU \n"; } } for(int c=0; c<47; c++){ cout << "="; } cout << "\n1. Add record " << endl; cout << "2. View players records " << endl; cout << "3. Compute for the average " << endl; cout << "4. Show the player(s) who gets the max average. " << endl; cout << "5. Show the player(s) who gets the min average. " << endl; cout << "6. Exit " << endl; cin >> choice; if (choice == 1){ cout << "Add player data" << endl; do { cout << "Enter player " << i << " nickname:" << endl; cin >> p1[i].name; cout << "Enter player " << i << " age:\n"; cin >> p2[j].age; cout << "Enter player " << i << " score 1:\n"; cin >> p2[j].score1; cout << "Enter player " << i << " score 2:\n"; cin >> p2[j].score2; cout << "Enter again? (Y/N)"; cin >> backtomain; i++; j++; } while(backtomain != 'N' && backtomain != 'n' && i < 6);{ goto main; } } if(choice == 2){ cout << "Player records" << endl; cout << "Player nickname" << "Player age" << "player score 1" << "player score 2\n"; for (z=1; z<= i-1; z++){ cout << p1[z].name << " " << p2[z].age << " "<< p2[z].score1 << " "<< p2[z].score2 << "\n"; } cout << "type and enter (Y/y) to go back to main menu" endl; cin >> backtomain2; if(backtomain2 !='Y' && backtomain2 !='y'); goto main; } if (choice == 3){ cout << "Players who got the max average" << endl; } return 0; } ``````
Hello peza,

I comment your use of the blank lines, but to many are just as bad as not enough. Also watch your indenting.

You should compile your code before posting and mention the complete error message the compiler returns. The ones that you do not understand.

Once I fix your errors I will be able to test the program.

Compare this with some changes I made:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118`` ``````#include #include #include using namespace std; // <--- Best not to use. struct playerdata { char name[50]; }; struct playerdata2 { int age, score1, score2; }; int main() { int choice, i = 1, j = 1, z = 1; char backtomain, backtomain2; playerdata p1[i]; // <--- Needs a constant like 10 or a variable defined as a constant. playerdata2 p2[j]; main: for (int a = 0; a < 47; a++) { cout << "="; } cout << "\n"; for (int b = 0; b < 22; b++) { cout << " "; if (b == 21) { cout << "MENU \n"; } } for (int c = 0; c < 47; c++) { cout << "="; } cout << "\n" "1. Add record\n" // <--- The space at the end is pointless. "2. View players records\n" "3. Compute for the average\n" "4. Show the player(s) who gets the max average.\n" "5. Show the player(s) who gets the min average.\n" "6. Exit\n" " Enter choice: "; // <--- This space has a use. No (\n) or "endl" puts the prompt on the same line. cin >> choice; if (choice == 1) { cout << "Add player data" << endl; do { cout << "Enter player " << i << " nickname:" << endl; cin >> p1[i].name; cout << "Enter player " << i << " age:\n"; cin >> p2[j].age; cout << "Enter player " << i << " score 1:\n"; cin >> p2[j].score1; cout << "Enter player " << i << " score 2:\n"; cin >> p2[j].score2; cout << "Enter again? (Y/N)"; cin >> backtomain; i++; j++; } while (backtomain != 'N' && backtomain != 'n' && i < 6); { // <--- These are unnecessary and do not belong to the do/while loop. goto main; } } if (choice == 2) { cout << "Player records" << endl; cout << "Player nickname" << "Player age" << "player score 1" << "player score 2\n"; for (z = 1; z <= i - 1; z++) { cout << p1[z].name << " " << p2[z].age << " " << p2[z].score1 << " " << p2[z].score2 << "\n"; } cout << "type and enter (Y/y) to go back to main menu" endl; // <--- Missing something. cin >> backtomain2; if (backtomain2 != 'Y' && backtomain2 != 'y'); // <--- This (;) ends the if statement. goto main; // <--- Is this part of the if statement? } if (choice == 3) { cout << "Players who got the max average" << endl; } return 0; // <--- Not required, but makes a good break point for testing. }``````

In your menu you do not need a "cout" and "endl" for every line. The example is just 1 "cout" and all the individual lines of quoted strings is considered just 1 string and 1 (;) to end it all. This is much easier to work with and has the advantage of looking like what is sent to the screen.

"goto" bad form also best not to use most of the time. A do/while or while loop can replace this. "goto"s that jump forward still have an occasional use.

Andy

Hello peza,

I have to ask the questions:

Why a C string and not a "std::string"?

Why 2 structs when 1 will do the job?

Have you learned functions yet and switch/case?

Using a "std::string" has many benefits.

Andy
For a revised starter, consider:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123`` ``````#include #include #include #include #include constexpr size_t MaxName {50}; struct playerdata { char name[MaxName] {}; int age {}, score1 {}, score2 {}; double avg {}; }; int getInt(const std::string& prm) { const auto notsp {[&]() {while (std::isspace(std::cin.peek()) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; }}; int i {}; while ((std::cout << prm) && (!(std::cin >> i) || notsp())) { std::cout << "Not an integer\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits::max(), '\n'); } std::cin.ignore(std::numeric_limits::max(), '\n'); return i; } int main() { constexpr size_t MaxPlay {6}; int choice {}; size_t play {}; double lwst {std::numeric_limits::max()}, hgst {std::numeric_limits::min()}; // THIS IS NOT STANDARD C++ //playerdata p1[i]; playerdata p1[MaxPlay] {}; do { std::cout << std::setw(47) << std::setfill('=') << "=" << '\n'; std::cout << std::setw(22) << std::setfill(' ') << ' ' << "MENU\n"; std::cout << std::setw(47) << std::setfill('=') << "=" << '\n' << std::setfill(' '); std::cout << "1. Add record\n"; std::cout << "2. View players records\n"; std::cout << "3. Compute for the average\n"; std::cout << "4. Show the player(s) who gets the max average\n"; std::cout << "5. Show the player(s) who gets the min average\n"; std::cout << "6. Exit\n"; char backtomain {}; switch (choice = getInt("Enter option: ")) { case 1: std::cout << "Add player data\n"; do { std::cout << "Enter player " << play + 1 << " nickname (no spaces): "; std::cin >> std::setw(MaxName) >> p1[play].name; p1[play].age = getInt("Enter player " + std::to_string(play + 1) + " age: "); p1[play].score1 = getInt("Enter player " + std::to_string(play + 1) + " score 1: "); p1[play].score2 = getInt("Enter player " + std::to_string(play + 1) + " score 2: "); p1[play].avg = (p1[play].score1 + p1[play].score2) / 2.0; if (p1[play].avg > hgst) hgst = p1[play].avg; if (p1[play].avg < lwst) lwst = p1[play].avg; std::cout << "Enter again (Y/N): "; std::cin >> backtomain; std::cin.ignore(std::numeric_limits::max(), '\n'); ++play; } while (std::toupper(static_cast(backtomain)) != 'N' && play < MaxPlay); break; case 2: std::cout << "Player records\n"; std::cout << std::left << std::setw(15) << "Nickname" << std::setw(5) << "Age" << std::setw(10) << "Score 1" << "Score 2\n"; for (int z = 0; z < play; ++z) std::cout << std::left << std::setw(15) << p1[z].name << std::setw(5) << p1[z].age << std::setw(10) << p1[z].score1 << p1[z].score2 << '\n'; break; case 3: // Done as part of 1) break; case 4: std::cout << "Players who got the max average\n"; for (size_t i = 0; i < play; ++i) if (p1[i].avg == hgst) std::cout << p1[i].name << '\n'; std::cout << '\n'; break; case 5: std::cout << "Players who got the min average\n"; for (size_t i = 0; i < play; ++i) if (p1[i].avg == lwst) std::cout << p1[i].name << '\n'; std::cout << '\n'; break; case 6: break; default: std::cout << "Unknown option\n"; break; } } while (choice != 6); }``````

Last edited on
Hello, thanks for the corrections and I sincerely apologize because I have no prior knowledge in using the "std::" format when it comes to coding. I will start learning and practicing it later, thank you again for the help
Topic archived. No new replies allowed.