Reading file into Vector and printing
Feb 10, 2012 at 12:30am UTC
I am getting a few errors, but one that I think might fix most of them is being able to fix the error that says "StudentProfile' was not declared in this scope", this error is showing at lines 13, 18, 26, 37, and 58.
I am sure my code needs more help than just that fix, but if someone can help me determine how to fix that error so I can continue to work on it, that would be great.
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void PrintInfo(vector<StudentProfile> v);
void getInfo(vector<StudentProfile>& v);
int main()
{
vector<StudentProfile> StCollection;
getInfo(StCollection);
PrintInfo(StCollection);
return 0;
}
void getInfo(vector<StudentProfile>& v)
{
ifstream fin;
fin.open("studentinfo.txt" );
if (!fin)
cout << "The file does not exist." << endl;
else
{
while (!fin.eof())
{
StudentProfile new_entry;
fin >> new_entry.PersonalInfo.ssn;
fin >> new_entry.PersonalInfo.fname;
fin >> new_entry.PersonalInfo.lname;
fin >> new_entry.PersonalInfo.age;
fin >> new_entry.PersonalInfo.gender;
fin >> new_entry.StdInfo.stno;
fin >> new_entry.StdInfo.Course1.coursenum;
fin >> new_entry.StdInfo.Course1.coursename;
fin >> new_entry.StdInfo.Course2.coursenum;
fin >> new_entry.StdInfo.Course2.coursename;
fin >> new_entry.StdInfo.Course3.coursenum;
fin >> new_entry.StdInfo.Course3.coursename;
v.push_back(new_entry);
}
}
}
void PrintInfo(vector<StudentProfile> v)
{
for (int i = 0; i < v.size(); i++)
{
cout << "SSN: " << v[i].PersonalInfo.ssn << endl;
cout << "First Name: " << v[i].PersonalInfo.fname << endl;
cout << "Last Name: " << v[i].PersonalInfo.lname << endl;
cout << "Age: " << v[i].PersonalInfo.age << endl;
cout << "Gender: " << v[i].PersonalInfo.gender << endl;
cout << "Student ID: " << v[i].StdInfo.stno << endl;
cout << "Course 1: " << v[i].StdInfo.Course1.coursename << v[i].StdInfo.Course1.coursenum << endl;
cout << "Course 2: " << v[i].StdInfo.Course2.coursename << v[i].StdInfo.Course2.coursenum << endl;
cout << "Course 3: " << v[i].StdInfo.Course3.coursename << v[i].StdInfo.Course3.coursenum << endl;
}
}
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;
};
class StudentProfile
{
public :
Person PersonalInfo;
Student StdInfo;
};
Feb 10, 2012 at 12:35am UTC
Do you use header files?
Feb 10, 2012 at 4:34am UTC
Like @sea noob said, you should put your classes in header files. But for now this compiles, move the classes to the top of the program. You can't use the class before they are defined.
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
// Must define the classes before you start to use them
// Move them into a header later
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;
};
class StudentProfile
{
public :
Person PersonalInfo;
Student StdInfo;
};
void PrintInfo(vector<StudentProfile> v);
void getInfo(vector<StudentProfile>& v);
int main()
{
vector<StudentProfile> StCollection;
getInfo(StCollection);
PrintInfo(StCollection);
return 0;
}
void getInfo(vector<StudentProfile>& v)
{
ifstream fin;
fin.open("studentinfo.txt" );
if (!fin)
cout << "The file does not exist." << endl;
else
{
while (!fin.eof())
{
StudentProfile new_entry;
fin >> new_entry.PersonalInfo.ssn;
fin >> new_entry.PersonalInfo.fname;
fin >> new_entry.PersonalInfo.lname;
fin >> new_entry.PersonalInfo.age;
fin >> new_entry.PersonalInfo.gender;
fin >> new_entry.StdInfo.stno;
fin >> new_entry.StdInfo.Course1.coursenum;
fin >> new_entry.StdInfo.Course1.coursename;
fin >> new_entry.StdInfo.Course2.coursenum;
fin >> new_entry.StdInfo.Course2.coursename;
fin >> new_entry.StdInfo.Course3.coursenum;
fin >> new_entry.StdInfo.Course3.coursename;
v.push_back(new_entry);
}
}
}
void PrintInfo(vector<StudentProfile> v)
{
for (int i = 0; i < v.size(); i++)
{
cout << "SSN: " << v[i].PersonalInfo.ssn << endl;
cout << "First Name: " << v[i].PersonalInfo.fname << endl;
cout << "Last Name: " << v[i].PersonalInfo.lname << endl;
cout << "Age: " << v[i].PersonalInfo.age << endl;
cout << "Gender: " << v[i].PersonalInfo.gender << endl;
cout << "Student ID: " << v[i].StdInfo.stno << endl;
cout << "Course 1: " << v[i].StdInfo.Course1.coursename << v[i].StdInfo.Course1.coursenum << endl;
cout << "Course 2: " << v[i].StdInfo.Course2.coursename << v[i].StdInfo.Course2.coursenum << endl;
cout << "Course 3: " << v[i].StdInfo.Course3.coursename << v[i].StdInfo.Course3.coursenum << endl;
}
}
Topic archived. No new replies allowed.