Pointers to structures

Pages: 12
Oct 7, 2019 at 1:54pm
Hello guys, I really need some help with this. I got it fully working with an array of structs, but now having an issue using pointers. We are not allowed to use [] anywhere except the initial declaration of the array. Our class lecture didn't really discuss all this and my book has one example not using loops. I have hidden most of the code so that I can go step by step to get it working first to read in, then print and all that. I have no idea how to bubble sort with pointers. I promise I have been doing research online on my own first, reading my book, watching lectures before coming on here.


We are supposed to be reading in a player's name (I split it into first and last), their player id, and their scores [7]. We then calculate each player's total score, the entire team's score, and the person with the highest score.

***please please try to be patient with me as I just learned pointers this week and I don't have a perfect grasp on them because the structures we learned last week and there isn't really good examples of pointers and structures with infiles or multiple values and all. Don't be mean please***

I thought I could get it working with basic arrays and then change everything to pointers. I have notes throughout. Just some guidance would be nice.

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  struct team
  {
      string FName;
      string LName;
      int player_ID;
      int scores[7];
      int totals;
  };
  
      
  /****************************/
  //function parameters
  void readPlayers(team *);
  int calculateScores(team *, int, int);
  void header( );
  int highestScore(team *, string&, string&);
  void sort(team *);
  int teamTotal(team *);
  void printResults(team *);
  /****************************/
  
  
  //functions 
  void readPlayers(team *teamptr)  //reading in all of the player's stats 
  {
      int i, j;
      int sum=0;
       for(i=0;i<12;i++)
      {
          infile>>teamptr->FName>>teamptr->LName;
          infile>>teamptr->player_ID;
         
         // for(j=0;j<7;j++)  //hidden for now cause I'm assuming I need to
        // {                  //make a second ptr for scores[j]??
            //  infile>>teamptr->scores[j]; 
        //  }
          teamptr++;
      }
  }
  
  void header( )  //the table header 
  {
      cout<<setw(10)<<"Name"<<setw(25)<<" Player ID"<<setw(35)<<" Total Points Scored"<<endl;
      cout<<"-------------------------------------------------------------------------"<<endl;
  }
  

//I know this doesn't work, I didn't know how to use pointers with it 
 void sort(team players[])  //sorting all the names alphabetically
{
  int i;
  team temp;
   bool swap;
   
  do
 {
    swap=false;
     
      for(i=0;i<12-1;i++)
    {
        if(players[i].LName>players[i+1].LName)
         {
            temp.FName=players[i].FName;
            temp.LName=players[i].LName;
            temp.player_ID=players[i].player_ID;
            temp.totals=players[i].totals;
           players[i].FName=players[i+1].FName;
           players[i].LName=players[i+1].LName;
           players[i].player_ID=players[i+1].player_ID;
           players[i].totals=players[i+1].totals;
           players[i+1].FName=temp.FName;
           players[i+1].LName=temp.LName;
          players[i+1].player_ID=temp.player_ID;
          players[i+1].totals=temp.totals;
          swap=true;
         }  
    }
    
 
  
  } while(swap);
  
 
  }
  
 int calculateScores(team *teamptr, int min, int max)  //adding up the total points scored for each player
 {
      int i, j;
     int sum=0;
      
     for(i=min;i<max;i++)
     {
         for(j=0;j<7;j++)
         {
             sum=sum+teamptr->scores[j];   //I'm sure this is wrong 
         }
         return sum;
      }
      
  }
  //the book shows something like freshman.name to print, but I keep getting an error 
//about member classes so it will say successful when I do this, but nothing prints. 
 void printResults(team *teamptr)  //printing the results of the players 
 {
     int i, j;
      for(i=0;i<12;i++)
 {
      cout<<setw(10)<<teamptr->FName<<" "<<teamptr->LName<<" "<<setw(18)<<teamptr->player_ID<<endl;
      
       cout<<endl;
       
       teamptr++;
  }
  }

    
  int teamTotals(team *teamptr) //the totals points for the team 
  {
     int sum=0;
      int i;
      
      for(i=0;i<12;i++)
     {
        sum=sum+teamptr->totals;
     }
      return sum;
 }
  
 int highestScore(team *teamptr, string &highPersonF, string &highPersonL)  //finding the player with the highest score 
  {
     int i, j;
    int highest=teamptr->totals;
     for(i=0;i<13;i++)
    {
         if(teamptr->totals>highest)
         {
          highest=teamptr->totals;
          highPersonF=teamptr->FName;
          highPersonL=teamptr->LName;
          }
      }
        return highest;
  }
  
  int main( ) //beginning of main 
  {
  
      infile.open("C:\\data\\input\\SoccerScores.txt");
      
      if(!infile)
      {
          cout<<"File did not open. Please try again.";
          return 0;
      }
      
      int i,j;
      int sum=0;
      int total=0;
      int teamTotal=0;
      int highest_score;
      string highPersonF, highPersonL;
  
      team players[12];
      team *teamptr;
      teamptr=players;
    
      header();
      readPlayers(teamptr);
      
      //calculating each player's individual total points 
      for(i=0;i<12;i++)
      {
          for(j=1;j<13;j++)
          {
              
      total=calculateScores(teamptr, i, j);
     teamptr->totals=total;
   
         }
     }
      //sorting the players by name 
      sort(teamptr);
      
      //printing the chart with all the results 
     printResults(teamptr);
      
      //calculating the team's total score 
     teamTotal=teamTotals(teamptr);
     cout<<endl;
     cout<<"The total points scored by the soccer team was: "<<teamTotal<<" pts"<<endl;
      
     highest_score=highestScore(teamptr, highPersonF, highPersonL);
     cout<<"The player with the highest score was "<<highPersonF<<" "<<highPersonL<<" with "<<highest_score<<" pts"<<endl;
      
  
  return 0;
  }
   
Last edited on Oct 7, 2019 at 1:55pm
Oct 7, 2019 at 2:00pm
When hiding the scores part so far I have:


    Name                Player ID                Total Points Scored
-------------------------------------------------------------------------
       Hop Along               1313

      Turn Anrun               9211

    Thomas Beesbie               2313

      Lite Foot               7322

      Ball Masher               7291

    Goldie Oldie               5345

       Big Phoot               8224

     Slick Slider               6342

         T Threader               7282

   Twinkle Toes               3824

   Stopper Topper               8421

    Golden Tow               3821


The total points scored by the soccer team was: 0 pts
The player with the highest score was   with 0 pts

Oct 7, 2019 at 2:01pm
there are different syntaxes for pointers that all do the same thing.
if you have
int *x = new int[1000];
then you can say
x[10] = 11; //like an array, but you were saying you cannot use this.
this is the same exact thing:
*(x+10) = 11;
its ugly, but its what the teacher asked for. There are a couple of other ways to do it, but this one is closest to the idea of array indexing.

with a struct, its going to be sorting off one field:

if( *(pointer+offset).field < tmp) //this is the syntax you will use. prettier is pointer[offset].field but you were handicapped.
swap as before in array type bubble sort.

pointers...
are exactly like array index.
double d[100];
int index = 10;
d[index] = 3.1415;
here, index represents a pointer. print it out and get 10: its not your data, its just where your data is. so you have to dereference the pointer, by going to memory where your data is, to get the thing you really wanted. Conceptually, that is exactly how a pointer works. Its just an array index (into the computers RAM as if it were an array, actually). The rest of it is just syntax to express these ideas to the compiler. Every time pointers seem overwhelming, repeat that to yourself and then relax and it will become clearer, until with practice it is second nature.

people here are rarely mean if you are trying to learn and do.

structs (and classes are nearly the same thing) are just a way to make a home-made type that ties several things together into one thing, making it easier to work with. There is a lot to learn about them, but you will get there.
Last edited on Oct 7, 2019 at 2:11pm
Oct 7, 2019 at 2:12pm
What about for this? Do I declare two pointers since the struct has score[7] for 12 players?
Can you show an example?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void readPlayers(team *teamptr)  //reading in all of the player's stats 
  {
      int i, j;
      int sum=0;
       for(i=0;i<12;i++)
      {
          infile>>teamptr->FName>>teamptr->LName;
          infile>>teamptr->player_ID;
         
         // for(j=0;j<7;j++)  //hidden for now cause I'm assuming I need to
        // {                  //make a second ptr for scores[j]??
            //  infile>>teamptr->scores[j]; 
        //  }
          teamptr++;
      }
  }
Oct 7, 2019 at 3:45pm
yes, it looks like EACH struct has an array inside it, so that would become a pointer.
And, you have a pointer 'array' of the structs.

so you have something like this...


the question is where you want to allocate the memory for the internal pointers.
I am going to do that in main. You can move it somewhere else if you want. Later you can do it with a constructor, but for now (assuming you don't know constructors yet), do it where you allocate the team pointer 'array'.


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

main()
{
team *tp = new team[somesize]; //this is in main or some get memory function. 
for(int i = 0; i < somesize; i++)
  *(tp+i).scores = new int[7]; //like this
}

void readPlayers(team *teamptr)  //reading in all of the player's stats 
  {
      int i, j;
      int sum=0;
       for(i=0;i<12;i++)
      {
          infile>>teamptr->FName>>teamptr->LName;
          infile>>teamptr->player_ID;
         int * ip = teamptr->scores;  //this makes access simpler. its doable without but messy. 
         for(j=0;j<7;j++)  //hidden for now cause I'm assuming I need to
         {                  //make a second ptr for scores[j]??
               //infile>>teamptr->scores[j]; 
               infile>> *ip;  
               ip ++;
         }
          teamptr++;
      }
  }



to delete, you MUST do a loop for all the team pointer items and delete scores first, then you can delete the pointer of teams. Otherwise it leaks memory (most likely it will work fine even if wrong, but its critical to understand this). Once your struct has a destructor and constructor to handle scores for you, it is much cleaner.

in the back of your mind, call the pointer i made, "ip", an "iterator". This word will help you when you encounter that idea later.

by the way you can muddle all this into one for loop, as you put it together in your mind.
for(int*ip = teamptr->scores; ip < (ip+7); ip++)
infile>> *ip;

Last edited on Oct 7, 2019 at 3:54pm
Oct 7, 2019 at 4:35pm
You should also be sending the array size as a parameter to the functions that need it.

void readPlayers(team teamptr[], int arraySize) //See note below

I would use array notation in a function that expects an array, use the pointer syntax when passing a single instance, remember the compiler sees the two syntax's as equivalent.

Also what happens if the file contains fewer records that what you expect?

What happens if there is a read failure?
Oct 7, 2019 at 5:03pm
I was going to fix the array size thing once I got it all polished and working. Not a good policy I know, but since Im a newb, I have to see the number 12 for now.

I got the bubble sort working perfectly. It didn't like the syntax for if(*(teamptr+offset).field
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
void sort(team *teamptr)  //sorting all the names alphabetically
{
  int i, j;
  team temp;
   bool swap;
   
  do
 {
    swap=false;
      for(i=0;i<12;i++)
    {
          for(j=0;j<12-1;j++)
          {
               if((teamptr+j)->LName>(teamptr+j+1)->LName)
         {
            temp.FName=(teamptr+j)->FName;
            temp.LName=(teamptr+j)->LName;
            temp.player_ID=(teamptr+j)->player_ID;
            temp.totals=(teamptr+j)->totals;  
           (teamptr+j)->FName=(teamptr+j+1)->FName;
           (teamptr+j)->LName=(teamptr+j+1)->LName;
           (teamptr+j)->player_ID=(teamptr+j+1)->player_ID;
           (teamptr+j)->totals=(teamptr+j+1)->totals;
           (teamptr+j+1)->FName=temp.FName;
          (teamptr+j+1)->LName=temp.LName;
          (teamptr+j+1)->player_ID=temp.player_ID;
          (teamptr+j+1)->totals=temp.totals;
          swap=true;
         
         }  
          }
    }
 
  } while(swap);
  }



Now just to get the scores pointer working. I couldn't figure out what exactly to change to get that first part working you sent me:
1
2
3
4
5
6
main()
{
team *tp = new team[somesize]; //this is in main or some get memory function. 
for(int i = 0; i < somesize; i++)
  *(tp+i).scores = new int[7]; //like this
}


but I still was able to use the second part in the read function and it read all the data. My problem is, how do I transfer that to another function? I most likely need to fix it in main first and then declare it in the function header instead of declaring it in read?
This is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 void readPlayers(team *teamptr)  //reading in all of the player's stats 
  {
      int i, j;
      int sum=0;
       for(i=0;i<12;i++)
      {
          infile>>teamptr->FName>>teamptr->LName;
          infile>>teamptr->player_ID;
         
          int *scoreptr =teamptr->scores;
          
         for(j=0;j<7;j++)
        {
              infile>>*scoreptr;          //now need to get scoreptr to other functions to sum 
               scoreptr++;                 //and other things 
          }
          teamptr++;
      }
  }


It would be really helpful if you could show me using the variables I have declared. For some reason my compiler is not liking certain syntaxes as well. Thanks so much for being nice and all the help. People are usually very sarcastic and talk to me like I'm an idiot on here usually is why I begged for people to not be mean. Sadly, it happens a lot. I do everything I can on my own before asking for help.
Last edited on Oct 7, 2019 at 5:06pm
Oct 7, 2019 at 5:18pm
my mistake on the . vs ->
I prefer array notation and messed that piece up. You seem to have figured it out. I haven't used this syntax in a long while.

------------
ok, for the first part... in main you have
team players[12];
team *teamptr;
teamptr=players;

.... and I am assuming you wanted to, and did, change scores in the struct to int* instead of array of int (?) like this:

struct team
{
string FName;
string LName;
int player_ID;
int scores[7];
int * scores;
int totals;
};

Assuming you did:

I believe the assignment wants you to use new here:
team players[12];
team *teamptr = new team[12];
teamptr=players;
for(int i = 0; i < 12; i++)
teamptr[i].scores = new int[7]; //or some other syntax version of this.

if you kept scores as int scores[7] then you do not need that loop.

----------

but I still was able to use the second part in the read function and it read all the data. My problem is, how do I transfer that to another function? I most likely need to fix it in main first and then declare it in the function header instead of declaring it in read?

sorry, what did you declare in read? I did not fully understand this question.
int *scoreptr =teamptr->scores; //this is not CREATING anything. It is just a way to access what you already have.

Last edited on Oct 7, 2019 at 5:19pm
Oct 7, 2019 at 5:27pm
I didn't change it in the struct cause I figure that was the initial declaration of scores[7] and counts as the first. If I get points off for that its fine. We have not learned new with pointers is why I'm assuming that initial declaration was ok. I'll show snippets of what I have:

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
//before main
 
  struct team
  {
      string FName;
      string LName;
      int player_ID;
      int scores[7];
      int totals;
  };
  
//in main 
      team players[12];
      team *teamptr;
      teamptr=players;
      
     
     //have not declared the scoreptr or anything which is why I was concerned it 
    //needed to be declared here and not the read function. 
   
      header();
      readPlayers(teamptr);

//my read function
void readPlayers(team *teamptr)  //reading in all of the player's stats 
  {
      int i, j;
      int sum=0;
       for(i=0;i<12;i++)
      {
          infile>>teamptr->FName>>teamptr->LName;
          infile>>teamptr->player_ID;
         
          int *scoreptr=teamptr->scores;  //scoreptr is declared here
          
         for(j=0;j<7;j++)
        {
              infile>>*scoreptr; 
              
               scoreptr++;
          }
        
          teamptr++;
      }
  }

int calculateScores(team *teamptr, int min, int max)  //adding up the total points scored for each player
 {
      int i, j;
     int sum=0;
      
     for(i=min;i<max;i++)
     {
         for(j=0;j<7;j++)
         {
             sum=sum+teamptr->scores[j];  //needs to be replaced with *scoreptr
         }                                                  //I tried a few things but its not working. 
         return sum;
      }
      
  }
//another example where I need scoreptr
 int teamTotals(team *teamptr)
  {
     int sum=0;
      int i;
      
      for(i=0;i<12;i++)
     {
        sum=sum+teamptr->totals;  //I need scoreptr here to sum the totals 

        teamptr++;
     }
      return sum;
 }
  


I need to move scoreptr through all my different functions to perform the calculations, output, and other things. I tried declaring it in the function header and it says member class not part of team. What is the syntax since scoreptr is pointing to an already pointer? I hope that explains what I'm trying to do. Yeah its not creating something new, but can't figure out the syntax for the sum of each player or the sum of all players using pointers. I already had this entire things done with arrays, but we have to use pointers so it has been a challenge
Last edited on Oct 7, 2019 at 5:31pm
Oct 7, 2019 at 5:33pm
ok, then ignore the thing about allocating with new, and the scores stuff. I misunderstood where you were on this.

scoreptr is just an accessor. Its not a "thing". **** details below.
When you are done with that loop, teams[x].score[y] has values.
so you can pass teamptr around and get access to all scores of all teams.

calculateScores what does this return? do you want the sum of the scores (1-7) for 1 team, or the sum of the scores 1-7 for all teams in one huge total?


scorepointer is functioning as a reference (not quite a c++ reference but nearly) or an 'alias' for each teams score array. It *IS* teamptr[x].scores. Its just a syntax sugar to make your file reading loop easier. It goes away, but teamptr's scores are still there, loaded from the file.

in technical terms, scoreptr = teams[x].score is saying that scoreptr points to the start of the array score in memory. So scoreptr IS score[0] the first iteration. then you ++ it and it IS score[1].

sum=sum+teamptr->totals; //I need scoreptr here to sum the totals
you don't.
you want
sum += *(teamptr->scores + i); //I think?
Last edited on Oct 7, 2019 at 7:09pm
Oct 7, 2019 at 8:45pm
Why does calculateScores() take a min and max index? Does it calculate scores for the players in that range of indices? And when you call it within main, it's inside a loop, as though it's supposed to compute the score for just one player. But in that case why does it take indexes at all?

To avoid this sort of confusion, I strongly recommend writing a comment when you declare the function that describes what that function does. This draws a line in the sand saying "this is the interface." Then you write the function to do what it says, AND you make sure that you call it the way it's intended.

In this case, I'd make calculateScores() compute the total score for just one player. Then you can call it on each player within main:
1
2
3
4
5
6
7
8
9
10
11
12
// Compute the total score for the player. I changed this to return void
void calculateScores(team *teamptr)
 {
     int sum=0;
     int *sc = teamptr->scores;
     for(i=0; i<7;i++)
     {
             sum=sum+ *sc;
             sc++;
      }
      teamptr->totals = sum;      
  }


And then in main():
1
2
3
4
5
6
// Calculate the total score for each player
team *tp = players;
for (i = 0; i<12; i++) {
    calculateScores(tp);
    tp++;
}


Then to get the total score of the whole team:
1
2
3
4
5
team *tp = players;
int totalScore = 0;
for (i = 0; i<12; i++) {
    totalScore = totalScore + tp->totals;
}


Also there's still a bug in your bubble sort: you don't swap the scores. You can fix this and simplify the code by using a dummy "team" variable to do the swap:
1
2
3
4
5
6
7
8
   ...
   if((teamptr+j)->LName>(teamptr+j+1)->LName)
         {
            team dummy = *(teamptr+j);
            *(teamptr+j) = *(teamptr+j+1);
            *(teamptr+j+1) = dummy;
            swap=true;
         }  
Oct 7, 2019 at 9:56pm
I did it as the min/max cause that is how I learned to do individual rows for 2D arrays. Idk, its easier for me to comprehend I guess. I tried implementing what you had written about changing it to void to print and copied you verbatim, but it kept giving weird errors. Idk why, but every suggestion about the * works differently on my complier? I finally got the right output. If it wern't for you guys, I'd be in a bind. Thank you for all your help and suggestions. I keep learning new things everyday.

Thank you thank you!
Oct 8, 2019 at 12:49pm
every suggestion about the * works differently on my complier? I finally got the right output.

This suggests that you don't understand how your program is working, or that it works by accident. I urge you to post the working code. We can look it over and hopefully help you better understand pointers.

BTW, this sort of assignment is really contrived. You'd never have to do this in the real world.
Oct 8, 2019 at 1:42pm
part of the trouble is me, trying to help with a syntax I don't use or like :)
I think I mangled a couple of them up there somewhere. My apologies.
Last edited on Oct 8, 2019 at 1:43pm
Oct 8, 2019 at 6:15pm
That's ok guys, thanks for helping. I failed the homework assignment though sadly. I wasn't allowed to use i anywhere. I thought I just wasn't allowed to use [] as per the instructions...

I didn't change some of the stuff like on the sort just because I was running out of time and wanted the right input to come out. Thanks to everyone that helped.
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
  #include <iomanip>
  #include <fstream>
  
  using namespace std;
  
  ifstream infile;
  ofstream outfile;
  
  struct team
  {
      string FName;
      string LName;
      int player_ID;
      int scores[7];
      int totals;
  };
  
      
  /****************************/
  //function parameters
  void readPlayers(team *, int size);
  int calculateScores(team *, int, int);
  void header( );
  int highestScore(team *, string&, string&, int size);
  void sort(team *, int size);
  int teamTotal(team *, int size);
  void printResults(team *, int size);
  /****************************/
  
  
  //functions 
  void readPlayers(team *teamptr, int size)  //reading in all of the player's stats 
  {
     int i, j;
      int sum=0;
       for(i=0;i<size;i++)
      {
          infile>>teamptr->FName>>teamptr->LName;
          infile>>teamptr->player_ID;
         
          int *scoreptr=teamptr->scores;  //scoreptr is declared here
          
         for(j=0;j<7;j++)
        {
              infile>>*scoreptr; 
               scoreptr++;
          }
        
          teamptr++;
      }
  }
  
  void header( )  //the table header 
  {
      outfile<<setw(10)<<"Name"<<setw(25)<<" Player ID"<<setw(35)<<" Total Points Scored"<<endl;
      outfile<<"-------------------------------------------------------------------------"<<endl;
  }
  
 void sort(team *teamptr, int size)  //sorting by last name and the other info follows 
{
  int i, j;
  team temp;
   bool swap;
   
   do
  {
    swap=false;
      for(i=0;i<size;i++)
    {
          for(j=0;j<size-1;j++)
          {
              if((teamptr+j)->LName>(teamptr+j+1)->LName)
              {
                temp.FName=(teamptr+j)->FName;
                temp.LName=(teamptr+j)->LName;
                temp.player_ID=(teamptr+j)->player_ID;
                temp.totals=(teamptr+j)->totals;
                (teamptr+j)->FName=(teamptr+j+1)->FName;
                (teamptr+j)->LName=(teamptr+j+1)->LName;
                (teamptr+j)->player_ID=(teamptr+j+1)->player_ID;
                (teamptr+j)->totals=(teamptr+j+1)->totals;
                (teamptr+j+1)->FName=temp.FName;
                (teamptr+j+1)->LName=temp.LName;
                (teamptr+j+1)->player_ID=temp.player_ID;
                (teamptr+j+1)->totals=temp.totals;
                 swap=true;
             }  
         } 
    }
  } while(swap);
 }
  
 int calculateScores(team *teamptr, int min, int max)  //adding up the total points scored for each player
 {
      int i, j;
     int sum=0;
     int *scoreptr=(teamptr+i)->scores;
     
     for(i=min;i<max;i++)
     {
         for(j=0;j<7;j++)
         {
             sum=sum+*scoreptr;
             scoreptr++;
         }
         return sum;
      }
      
  }
  
 void printResults(team *teamptr, int size)  //printing the results of the players 
 {
     int i, j;
      for(i=0;i<size;i++)
 {
      outfile<<setw(10)<<teamptr->FName<<" "<<teamptr->LName<<" "<<setw(18)<<teamptr->player_ID<<" "<<
              setw(20)<<teamptr->totals<<endl;
      
       outfile<<endl;
       
       teamptr++;
  }
  }

    
  int teamTotals(team *teamptr, int size)  //find the total score for the entire team 
  {
     int sum=0;
      int i;
      
      for(i=0;i<size;i++)
     {
        sum=sum+teamptr->totals;
        teamptr++;
     }
      return sum;
 }
  
 int highestScore(team *teamptr, string &highPersonF, string &highPersonL, int size)  //finding the player with the highest score 
  {
     int i, j;
    int highest=(teamptr+0)->totals;
     for(i=0;i<size;i++)
    {
         if((teamptr+i)->totals>highest)
         {
          highest=(teamptr+i)->totals;
          highPersonF=(teamptr+i)->FName;
          highPersonL=(teamptr+i)->LName;
          }
      }
        return highest;
  }
  
  int main( ) //beginning of main 
  {
  
      infile.open("C:\\data\\input\\SoccerScores.txt");
      outfile.open("C://data//soccerStats.txt");
      
      if(!infile)
      {
          cout<<"File did not open. Please try again.";
          return 0;
      }
      
      int i,j;
      int sum=0;
      int size=12;
      int total=0;
      int teamTotal=0;
      int highest_score;
      string highPersonF, highPersonL;
  
      team players[size];
      team *teamptr;
      teamptr=players;
    
   
      header();
	  
      readPlayers(teamptr, size);
      
      //calculating each player's individual total points 
      for(i=0;i<size;i++)
      {
          for(j=1;j<13;j++)
          {    
      total=calculateScores(teamptr, i, j);
      (teamptr+i)->totals=total;
         }
     }
      //sorting the players by name 
     sort(teamptr, size);
      
      //printing the chart with all the results 
     printResults(teamptr, size);
      
      //calculating the team's total score 
     teamTotal=teamTotals(teamptr, size);
     outfile<<endl;
     outfile<<"The total points scored by the soccer team was: "<<teamTotal<<" pts"<<endl;
      
     highest_score=highestScore(teamptr, highPersonF, highPersonL, size);
     outfile<<"The player with the highest score was "<<highPersonF<<" "<<highPersonL<<" with "<<highest_score<<" pts"<<endl;
return 0;
  }

Last edited on Oct 8, 2019 at 6:17pm
Oct 8, 2019 at 6:44pm
He only took 2 points off on my homework so I am thankful, would like to try to see what I did wrong for next time though.
Oct 8, 2019 at 6:47pm
would like to try to see what I did wrong for next time though.

Did you ask for clarification?

Oct 8, 2019 at 9:47pm
Yes he said: "Your assignment was to use pointers. I indicated that I do not want to see syntax (temptr +i) where i is a loop control variable. This defeats the purpose of using pointers over array syntax with subscripts".
Oct 8, 2019 at 10:25pm
Okay since he specifically told you not to use that syntax??

Note it would have been nice to know this from the first post then you could have gotten guidance respecting this limitation.



By the way this: team players[size]; is not legal in C++, array sizes must be compile time constants, size is not a constant.

Oct 9, 2019 at 12:26am
maybe he wanted the iterator style.

for(int*ip = teamptr->scores; ip < (ip+7); ip++)
infile>> *ip;

Pages: 12