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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

#include<iostream>
#include<cstring>
#include<cctype>

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
#include<cstring>
#include<cctype>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <cctype>

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<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main() {
	constexpr size_t MaxPlay {6};
	int choice {};
	size_t play {};

	double lwst {std::numeric_limits<double>::max()}, hgst {std::numeric_limits<double>::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<std::streamsize>::max(), '\n');

					++play;
				} while (std::toupper(static_cast<unsigned char>(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.