I have a vector
ofstructs
.
I need to sort the vector in alphabetical order according to the last name of the student in eachstruct
in thestudents
vector. Is this possible? Each student in thestruct
has the following information:
struct Student
{
string lastName;
string firstName;
string stdNumber;
double assgn1;//doubles are what I want to add in another function.
double assign2;
double assign3;
double assign4;
double midTerm;
double finalGrade;
bool operator<(Student const &other) const {
return lastName < other.lastName;
}
};
This is where my students
vector is made and filled:
int getFileInfo()
{
int failed=0;
ifstream fin;
string fileName;
vector<Student> students;// A place to store the list of 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){
cout<<"Reading "<<s.firstName<<" "<<s.lastName<<" "<<s.stdNumber<<endl;
students.push_back(s);
}
vector<Student>::iterator loop = students.begin();
vector<Student>::iterator end = students.end();
fin.close();
return 0;
}
So I want to sort thestruct
on the last name, and then be able to manipulate each “Student”
struct` in the vector in ANOTHER FUNCTION. Is this possible to do?
I want to be able to able to add the double
parts to each student in the vector that I have in another function. I then want to be able to print out all of the information of each student. I can print out the information of each student if I do it within the function that the students
vector is in, but I need to print in another function, void gradeInput()
. I will cin>>
each double
grade for each student, one student at a time. I was thinking it would look something like this:
void gradeInput()
{
For(each student)// I don’t know what to do to in this for loop to loop through
//each student. I want to make it so everywhere “stud” is change to the
//next student after one loop iteration.
//I made a default `Student` called ‘stud’ to show this example..
cout<<"Student "<<stud.firstName<<" "<<stud.lastName<<" "<<stud.stdNumber<<":";
cout<<"Please, enter a grade (between 0.0 and 100.0) for ... "<<endl;
cout<<"Assignment 1:";
cin>>stud.assgn1;
cout<<endl;
cout<<"Assignment 2:";
cin>>stud.assign2;
cout<<endl;
cout<<"Assignment 3:";
cin>>stud.assign3;
cout<<endl;
cout<<"Assignment 4: ";
cin>>stud.assign4;
cout<<endl;
cout<<"MidTerm: ";
cin>>stud.midTerm;
cout<<endl;
cout<<"Final Exam: ";
cin>>stud.finalGrade;
cout<<endl;
return;
}
Hopefully this makes some sense, and I can get some help! My teacher isn’t helping me much as she doesn’t respond to emails, and has 45 min office hours, so all you friendly folks are a great asset! Thanks!
P.S. Sorry for the poor code formatting, I'm still trying to figure out the code input of the site.