Storing a linked list into a vector?

I have to store data from a file into a struct with linked list in it. This is what I've done so far

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
struct Class 
{
 int term; 
 string subject; 
 string catalog; 
 string letGrade; 
 double numGrade;  
 Class* next;
};
typedef Class* ClassList;

struct Student
{
 string ID; 
 ClassList ClassesTaken;// a linked list of Class objects

...

void loadData (ifstream& IN, vector<Student>& sVec)
{
 IN.open("file.tsv");
 if (IN.fail())
 {
  cout << "file has failed to open." << endl;
 }

 getID (IN, sVec);
}

void getID (ifstream& IN, vector<Student>& sVec)
{ 
 vector<string> nameVec;
 int term_num;
 string sub, cata, grade_let;
 double grade_num;

 while (IN >> s.ID >> term_num >> sub >> cata >> grade_let >> grade_num)
 {
   insertName (s.ID, nameVec);
   createList (IN, term_num, sub, cata, grade_let, grade_num);
 }
}

void insertName (string id, vector<string>& nameVec)
{
 nameVec.push_back (s.ID);
}

void createList (ifstream& IN, int term_num, string sub, string cata, string grade_let, double grade_num)
{
 Class *top = new Class;
 top -> term = term_num;
 top -> subject = sub;
 top -> catalog = cata;
 top -> letGrade = grade_let;
 top -> numGrade = grade_num;
 top -> next = storage;
 storage = top;
}


I've successfully gathered the info but now I'm not sure how to use push_back to store it into vector<Student> sVec. This is what I've tried but it doesn't work and I'm not sure what to do now. I get segmentation errors whenever I try to display the term to see whether I stored it in the structure correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void insertInVec (ifstream& IN, vector<Student>& sVec, vector<string>& nameVec)
{
 s.ClassesTaken = storage;
 Class info;

 for (int i = nameVec.size() - 1; i >= 0; i--) 
 // goes backwards to match ID with classes
 {
  s.ID = nameVec[i]; 
  info. term = s.ClassesTaken -> term;
 
  sVec.push_back(s);
  s.ClassesTaken = s.ClassesTaken -> next;
 }

 for (int j = 0; j < 5; j++)
 {
  cout << sVec[j].ID<< "	" << /* << s.Classes -> term <<*/ endl;
 }
}
Last edited on
bump
Topic archived. No new replies allowed.