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
#include<iostream>
#include<cstring>
#include<cctype>
usingnamespace 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.
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