The goal is to store the students names in an STL list and sort them based on the last name.
- Your primary tasks for this exercise are to edit the main.cpp file so that it does the following:
- inputs students’ names from input.txt into the STL list
- sorts the list based on the last name
- outputs the students’ names in order
- Steps include:
- Read each line from input.txt. Store the firstname and the lastname into the temporary student structure “currStudent”.
- Hint: You can use the following format:
while(studentFile >> currStudent.firstname >> currStudent.lastname)
- Push the current student into the list (“students”)
- Build and run the executable file. If you get an error like this:
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'DataType' (or there is no acceptable conversion)
- Notice that the compiler is complaining about the ‘<<‘. You are trying to output a DataType but, there is no existing definition for cout << DataType.
- Solve this problem by overloading the “<<” operator.
- Because the packets haven’t been sorted yet, your output should look like the following:
Jacob AndersonMichael ThomsonJoshua SmithMathew MatheisEthan Evans Emily DrakeEmma PattersonMadison McPheeHannah BriensAshley SchmidtPress any key to continue
- Add code to sort the list (students), and print the correctly ordered list of students (using the iterator).
- Build and run the executable. If you get this error:
error C2784: 'bool std::operator <(const std::list<_Ty,_Alloc> &, const std::list<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::list<_Ty,_Alloc> &' from 'DataType'
- Reread the description of sort. This error is complaining about the following comparison in the sort function: DataType < DataType
- When you correct this problem, consider using getKey(). Why?
- (later, if you wanted to sort by firstname, how would you do it?)
- Your output should look like the following:
Jacob AndersonMichael ThomsonJoshua SmithMathew MatheisEthan EvansEmily DrakeEmma PattersonMadison McPheeHannah BriensAshley SchmidtSorting.........Jacob AndersonHannah BriensEmily DrakeEthan EvansMathew MatheisMadison McPheeEmma PattersonAshley SchmidtJoshua SmithMichael ThomsonPress any key to continue
————————————————————————————————————————–
main.cpp
#include <iostream>#include <fstream>#include <string>#include <list>using namespace std;struct DataType { string lastname; // Student's Last Name string firstname; // Student's First Name string getKey () const { return lastname; } // Returns the key field};//---------Need to add code to overload operator <<//------- Need to add code to fix error C2784: 'bool __cdecl std::operator <bool operator < (DataType lhs, DataType rhs){}void main() { ifstream studentFile ("input.txt"); // Student file list <DataType> students; // Students DataType currStudent; // One Student (has firstname,lastname) // Read each line from input.txt: //Store the firstname and the lastname into //the temporary student structure "currStudent". // Push the current student into the list ("students") // Use an iterator to print the unsorted list of students. // Sort the list of students. // Use an iterator to print the sorted list of students.}
Is this the question you were looking for? Place your Order Here