For each line of text:
-Get first name
-Get last name
-Get Student Number
(There will be an unknown amount of lines, each looking the same in format.)
Format:
First Last 111111111
First Last 111111112
...
I want to put each piece of each student into its own struct. I’ve set up the struct as follows:
struct Student{
string lastName;
string firstName;
string stdNumber;
double assgn1;//doubles will be used later in prog.
double assign2;
double assign3;
double assign4;
double midTerm;
double finalGrade;
};
My file input function is as follows:
int getFileInfo()
{
int failed=0;
ifstream fin;
string fileName;
vector<Student> students;
Student s; // A place to store data of one student
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl;
do{
if(failed>=1)
cout<<"Please enter a correct filename."<<endl;
cin>>fileName;
fin.open(fileName.c_str());// Open the file
failed++;
}while(!fin.good());
while (fin >> s.firstName >> s.lastName >> s.stdNumber)
students.push_back(s);
fin.close();
return 0;
}
In the file input, I make a vector, but I am unsure how to access each individual part of it, or the student, s, because it seems that it only makes one student. I was told that each line of the file is split and inputted into the students vector, but I don’t know how to extract that information. How do I get each student out of the students vector and into its own struct so I can use each student as its own struct?
So in the end, I would want to be able to output:
Student1: First Last 111111111
Student2: First Last 111111112
However More students are in the file
Thanks in advance for the help!!
@Loki
I changed the loop so loop != end and I still get the same problem. Here is my code:
int getFileInfo()
{
int failed=0;
ifstream fin;
string fileName;
vector<Student> students;// A place to store the list of students
vector<Student>::iterator loop = students.begin();
vector<Student>::iterator end = students.end();
Student s; // A place to store data of one student
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl;
do{
if(failed>=1)
cout<<"Please enter a correct filename."<<endl;
cin>>fileName;
fin.open(fileName.c_str());// Open the file
failed++;
}while(!fin.good());
while (fin >> s.firstName >> s.lastName >> s.stdNumber){
cout<<"Reading "<<s.firstName<<" "<<s.lastName<<" "<<s.stdNumber<<endl;
students.push_back(s);
}
fin.close();
for(loop;loop!=end;++loop)
cout<<loop->firstName<<" "<<loop->lastName<<" "<<loop->stdNumber<<endl;
return 0;
}
Again, Visual studio is saying vector iterators incompatible,
Pointing to “C:\Program Files(x86)\Microsoft Visual Studio 10.0\vc\include\vector”
You can access individual elements via the operator[]
You can use an index from 0 -> size().
You are creating only one student ‘s’ (As a side not pick a better name). But when you call push_back() you are copying this student object into the vector. So the vector gets a copy of ‘s’ that it stores. Then each time through the loop you overwrite the old values in ‘s’ with new values retrieved from the file.
As noted above each element can be reached individually using the operator[]
Alternatively you can use iterators to get a range. of students.
You can access an element by de-referencing loop and move to the next student by incrementing loop.
If loop == end then you have moved past all the students.
The students are in the vector already in structs. you can use them directly as you would a struct outside. But if you want to copy them out you can do this:
Use std::cout to output stuff to the standard output. Or create an object of type std::ofstream to output to a file: