Structures with arrays help!

hello! i wrote this code, but i am having some issue with fiding the higiest value stored in p[i].points_scored corresponding with the player who scored it.
but I cant seem to find any answer online regarding how to point to the array in the structure in regards of the previously specified. The player who scored the highest.
help?

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

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct Player{
    string name;
    int player_number;
    int points_scored;
};

int highscore(Player p[],int players);

int main()
{
    int total_points=0;
   // Array of Player structures
    Player p[5];
   //counter
    int index=1;

   // Get each player's info.
    for(int i=0; i<5; i++) {

        cout << "\nPLAYER #" << (index++) << "\n";
        cout << "---------\n";

        cout <<"Player name: ";
        cin>>p[i].name;
        cout <<"Player's number: ";
        cin>>p[i].player_number;
        cout <<"Points scored: ";
        cin>>p[i].points_scored;
        total_points += p[i].points_scored;
    }


    cout << setw(20) << left << "\nNAME";
    cout << setw(10) << "NUMBER";
    cout << setw(10) << "POINTS SCORED\n";

    for(int i=0; i<5; i++) {
        // Display the team info.
        cout << setw(20) << left << p[i].name;

        cout << setw(10) << p[i].player_number;

        cout << setw(10) << p[i].points_scored<<endl;
    }


   // Display total
    cout << "TOTAL POINTS: "
    << total_points << endl;

   // Display the player scoring the most points.
    int max_score = highscore(p, 5);

    cout<<"The player who scored the most points is: "
    << max_score << endl;
    return 0;

}

//find the player with the highest score
int highscore(Player p[],int players){
    

        return max;
}


I know obviously that I have to do it with a loop. But, how do I point the array from the struct?
Last edited on
Sounds like your function shouldn't return the highest score. Sounds like your function should return the position in the array of the highest score.

Alternatively, don't return the highest score; return an entire Player object, which contains the score AND the name.
The function should do this:

1
2
3
4
5
6
7
8
9
10
11
12

//find the player with the highest score
int highscore(Player p[],int players){
    int max = -1;
    for(int i = 0; i < players; i++){
	if(p[i].points_scored > max){
	    max = i;
	}
    }
    return max;
}


Than, this will return where (into the array of players) is the one with the biggest score. So, to call it you should do this:

1
2
3
4
5
6
7
8

   // Get the position of the player scoring the most points.
    int max_score = highscore(p, 5);

    cout<<"The player who scored the most points is: "
    << p[max_score].name << endl; // you can display its number too
    return 0;


Hope it helps
Also, I assumed that there is no one with a negative score. If it is possible for that condition to happen, just change the max value into the function with another sentinel value.
//find the player with the highest score

Is the function meant to find the player with the highest score, or is it meant to find the highest score? What are the actual requirements here?
Topic archived. No new replies allowed.