1

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.

b3orion
  • 77
  • 3
  • 8

1 Answers1

0

Getting the sorting to work is pretty easy -- just define an operator< for that type:

class student {
    // ...

    bool operator<(student const &other) const { 
        return last_name < other.last_name;
    }
};

Note that you may want (for example) to use the student's first name as a secondary sort field, to be used only when/if the last names are identical.

As far as reading the data for a student goes, you normally want to do that in a function named operator>>, like:

std::istream &operator>>(std::istream &is, student &s) { 
    is >> student.last_name;
    is >> student.first_name;
    // read other fields;
    return is;
}

If at all reasonable, I'd avoid making the entry interactive as you have though. Although common in student assignments, it renders the program nearly unusable. Printing out the information is normally done with operator<<, which is essentially a mirror image of operator>> (i.e., it writes instead of reads, but should write the fields in the same order, in a format that operator>> can read).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • one month left of class and we haven't learned about classes D: ... so help me out here; Do I use a `class` instead of a `struct`? I saw you have commented the beginning of the class, I know I have to initialize and create some stuff in it, but I don't know what. Can you write some pseudo-code there and see if I can figure out what to do from that? Same with the `&operator` function, never used one before. Thanks-- – b3orion Nov 01 '11 at 05:44
  • No - you can leave it as a struct. Just define your `operator<` inside that struct. – Jerry Coffin Nov 01 '11 at 05:47
  • See http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c – Karl Knechtel Nov 01 '11 at 08:01
  • Okay, I think I put defined the `operator<` correctly, I added it to my code in the up in the question, does it look right? I don't understand where to put the next bit of code, the `&operator>>` part, can you help me with that? – b3orion Nov 01 '11 at 15:56
  • I'm willing to bet that they haven't gone over operator overloading yet either... – tafoo85 Nov 01 '11 at 15:57
  • @tafoo85, if you bet money, you better go and collect! But I figure once I get it explained to me, I can start to understand it and using it more in my code... – b3orion Nov 01 '11 at 18:05