Learning Structures, stuck reading in

Pages: 12
> if ((*ptr).goals > (*(ptr + 1)).goals)
Try > most.
You're trying to sort the array, not find the maximum.

> Info MostGoals(Info *ptr, Info *spa)
1. You don't return anything, so say void.

2. Using a parameter as a way of declaring local variables is just weird.

Your original post was a lot better than this.
In MostGoals, I'd store a pointer to the player with the most goals rather than several variables describing their statistics.

Also, take Salem C's advice: don't pass ptr into MostGoals() (or the other functions). It should be a local variable. And MostGoals should return void. That means you need to change the declaration of MostGoals near the top, the definition, and the call inside Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void MostGoals(Info*);
...
void MostGoals(Info *spa)
{
    Info *best = spa;           // start by assuming the first player is best
    for (Info *ptr = spa; ptr < (spa+PLAYERS); ptr++)
    {
        if (best->goals < ptr->goals)
        {
            best = ptr;
        }
    }
    out << left << setw(20) << "MVP" << "ID#"
        << right << setw(19) << "goals" << endl
        <<"---------------------------------------------\n"
        << left << setw(20) << best->name
        << best->ID << right << setw(16) << best->goals << endl;
}

int main()
{
...
    MostGoals(spa);


Once you have this working, make ptr a local variable in the other functions too.
Topic archived. No new replies allowed.
Pages: 12