Storing data from a file from multiple lines into different variables

Hi There, I have been doing files in c++ for about a month now and I have a grip on how to do basic questions. However I cam across this question that needed me to store different variables on each line and I have never done something like this before. Please can someone help. The data file looks like so:

3222 Peter Monyane
26 M 1
42 42 42 42 42
11000 11070 10960 10090 9090
2343 Sarah Sekonyane
31 F 0
21 21 21 42 21
5600 5499 5555 12000 6000

So for example I need to store Peters number, name, age, sex etc and then move onto the next person. I am looping through the file but am only able to store the first line of data and then I dont know how to store the second line of data into new variables. I dont need to be spoon fed but I have been on this for 12 days now and I still cant figure it out.

Looks like this is all the data representing one person:

1
2
3
4
3222 Peter Monyane
26 M 1
42 42 42 42 42
11000 11070 10960 10090 9090


Create a struct that can hold all that data. Something like:

1
2
3
4
5
6
7
8
9
10
struct dataset
{
  int number;
  string first_name;
  string last_name;
  int age;
  char sex;
  ...
  ...
};


Open the file, create a dataset, read into it;

1
2
3
4
5
6
7
8
9
10
11
12
13
vector<person> allThePeople;

dataset person;
fileIn >> person.number;
fileIn >> person.first_name;
fileIn >> person.second_name;
fileIn >> person.age;
fileIn >> person.sex;
// ETC ETC. READ IN ONE WHOLE SET OF DATA

// Now the variable "person" holds all the data for one person.
// Put that person object into some sensible container, like a vector<person>
allThePeople.push_back(person);


Now do it more than once. A loop. Keep filling person with all the data, and then copying that person into the vector, until all done.
Last edited on
Show what you've tried, and explain exactly what each line/item of the file means.

Registered users can post here. Sign in or register to post.