Can't figure out how to approach this problem [inputs, arrays, records]

Hi guys.

This forum has been ridiculously helpful in the past, and I'm back with my newest assignment.

So basically I'm having problems figuring out what kind of logical structure I need to display my input properly.

I have this input:
11 a 3 p 4 r 1
32 p8 a 1 r 3
44   r0   p10   a6
5 p20 r3    a 7
57 a5  p 10  r 1
32  r 4  a 2 p 10


The first integer represents the uniform number of a basketball player.
The a represents assists.
The p represents points.
The r represents rebounds.

The input file will always begin with an integer but as you can tell the rest of the line can begin with any character (a, p, or r). The spacing in between characters is also random.

So I can't figure out how to store a variable with its value since they can jumble around.

Here's my code:
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

struct play_record
{
  string position;                          // position played
  int uniform;                              // uniform number
  int games;                                // number of games played
  int assists;                              // number of assists
  int points;                               // number of points scored
  int rebounds;                             // number of rebouns
};

int main()
{
  ifstream infile;
  string filename;
  cout << "Please enter name of player stats file" << endl;
  cin >> filename;
  infile.open(filename.c_str());
  n=0;
  infile >> team[n].uniform;
  while (infile)
    {
      infile >> team[n].assists;             // What to do here??

      infile >> team[n].rebounds;

      infile >> team[n].points;
    }
  cout << " UNIFORM #     GAMES   ASSISTS    POINTS  REBOUNDS" << endl;

  cout << team[n].uniform << team[n].game << team[n].assists << team[n]points << team[n]rebounds << endl;

}

How can I read in the values and assign them to the correct variables?

Would it be something like

1
2
if (team[n] == 'a')
infile >> team[n].assists


etc?
Last edited on
There is a number of ways to solve this. What I'd do is read the whole file into a string, delete the spaces from the string, and read a number/char/number/char/etc.
This system relies on the fact that all the lines start with a number which is followed by a char-number pair. When you read the char use a switch statement to check if it is 'p', 'r', or 'a' and use that to assign the number to the corresponding variable

Remember to increment the index for 'tem' in the loop
Last edited on
I agree with Maeriden, read the file, remove spaces and then work with data.

I would consider writing 2 programs, or 2 functions, one will take the raw data and make it pretty, verify all the data is available, sorted and ready to output.
If one line has invalid numbers, letters, it would alert the user but continue to the next line.

The second would read the perfect file and manage stats, reports and output.
11, A#, P#, R#

The reason to do it this way is if your given multiple files for say 40 teams.
The first program sorts the data once, the second takes the polished data and spits out your report.

In the real world, you don't want to polish data more than once, but you might want to run multiple reports based on that data. no reason to clean it every time.




Topic archived. No new replies allowed.