classes in one file

So We are supposed to used classes and write a program so that it reads the file and then outputs everything in the vector. At first nothing pritned and then when i did test cases everything printed up until it got to course Number.

I feel that it has something to do with getting what's in the text file?

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>


using namespace std;

class Person
{
public:
  long SSN;
  string Fname;
  string Lname;
  int Age;
  char Gender;
};
//-----------------------                                                                                                                                                            
class Course
{
public:
                                                                                                                                                                 
  long CourseNum;
  string CourseName;
};
//------------------------                                                                                                                                                           
class Student
{
public:
  long StNo;
  Course Course1;
  Course Course2;
  Course Course3;
  Course Course4;
  Course Course5;

};
//-------------------------                                                                                                                                                          
class StudentProfile
{
public:
  Person PersonalInfo;
  Student StdInfo;

};
//________________________                                                                                                                                                           

int GetInfo(vector <StudentProfile>& Stcollection)
{
  StudentProfile new_student;
  ifstream inputFile;
  inputFile.open("newLab3.txt");
  int count=0;
  inputFile >> new_student.PersonalInfo.SSN >> new_student.PersonalInfo.Fname
            >> new_student.PersonalInfo.Lname >> new_student.PersonalInfo.Age
            >> new_student.PersonalInfo.Gender >> new_student.StdInfo.StNo
            >> new_student.StdInfo.Course1.CourseNum >> new_student.StdInfo.Course1.CourseName
            >> new_student.StdInfo.Course2.CourseNum >> new_student.StdInfo.Course2.CourseName
            >> new_student.StdInfo.Course3.CourseNum >> new_student.StdInfo.Course3.CourseName
            >> new_student.StdInfo.Course4.CourseNum >> new_student.StdInfo.Course4.CourseName;


  while(inputFile)
    {

      Stcollection[count]=new_student;
      count++;
      inputFile >> new_student.PersonalInfo.SSN >> new_student.PersonalInfo.Fname
                >>new_student.PersonalInfo.Lname >> new_student.PersonalInfo.Age
                >>new_student.PersonalInfo.Gender >> new_student.StdInfo.StNo
 >> new_student.StdInfo.Course1.CourseNum >> new_student.StdInfo.Course1.CourseName
                >> new_student.StdInfo.Course2.CourseNum >> new_student.StdInfo.Course2.CourseName
                >> new_student.StdInfo.Course3.CourseNum >> new_student.StdInfo.Course3.CourseName
                >> new_student.StdInfo.Course4.CourseNum >> new_student.StdInfo.Course4.CourseName;


    }
  inputFile.close();
  return(count);
}






void PrintStudents(const vector <StudentProfile>& Stcollection, int count)
{
  //  cout << "inside the printstudent";                                                                                                                                             
  for(int i=0; i<count; i++)
    {



      cout << "SSN: " << Stcollection[i].PersonalInfo.SSN << endl
           <<"First name: " << Stcollection[i].PersonalInfo.Fname << endl
           << "Last Name: " << Stcollection[i].PersonalInfo.Lname<< endl
           << "Age: " << Stcollection[i].PersonalInfo.Age << endl
          << "Gender: " << Stcollection[i].PersonalInfo.Gender << endl
           << "Student Number: " << Stcollection[i].StdInfo.StNo << endl
           << "Course 1 Num: " << Stcollection[i].StdInfo.Course1.CourseNum << endl
           << "Course1 Name: " << Stcollection[i].StdInfo.Course1.CourseName << endl
           << "Course2 Num: " << Stcollection[i].StdInfo.Course2.CourseNum << endl
           << "Course2 Name: " << Stcollection[i].StdInfo.Course2.CourseName << endl
           << "Course3 Num: " << Stcollection[i].StdInfo.Course3.CourseNum <<endl
           << "Course3 Name: " << Stcollection[i].StdInfo.Course3.CourseName << endl
           << "-------------------------------" <<endl;

    }
}
//------------------------------------------                                                                                                                                         

//--------------------------------------------                                                                                                                                       
int main()
{
  vector <StudentProfile> Stcollection;
  int NumOfStcollection = GetInfo( Stcollection);
  PrintStudents(Stcollection, NumOfStcollection);
}





The problem is that you create an empty vector on line 117. On line 67 you copy new_student to a non existing field of that vector which causes undefined behavior.

So either you call Stcollection.push_back(new_student); or you make Stcollection large enough to hold the data (not recommended).

Anyway copying new_student to the vector needs to be done after line 75.

By the way: In GetInfo(...) count is not needed because the vector has a size.

See:

http://www.cplusplus.com/reference/vector/vector/vector/
http://www.cplusplus.com/reference/vector/vector/push_back/
http://www.cplusplus.com/reference/vector/vector/size/
Topic archived. No new replies allowed.