In my program i have three sperate vectors that store the students last name, first and score. I need to sort it by last name and print the contents of the vectors lastname, firstname: score. Well i have got it doing that for the most part. But after it sorts the lastnames the first names and scores are still in the same postion in their vectors, so when it prints the reults the last names are with the wrong first name and score. any ideas?
Code:
#include <iostream> // allows the program to output data to the screen #include <conio.h> #include <iomanip> #include <cstdlib> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <fstream> #include <ios> #include <istream> #include <limits> #include "students.h" // gradebook class defintion using std::cout; // program uses cout using std::cin; // program uses cin using std::endl; // program uses endl using std::setprecision; // set numeric output precision using std::fixed; // ensures that decimal point is displayed using std::setw; using std::string; using std::vector; using std::max; void students::displayMessage() { cout << endl << "Welcome to the Student Scores Application." << endl << endl; } void students::getData() { int numStudents = 0; string name; int score = 0; int i = 0; cout <<"Enter number of students to enter: "; cin >> numStudents; cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); vector<string> student_lastnames; vector<string> student_firstnames; vector <int> student_score; do { cout << endl << "Student " << i + 1 <<" last name: "; cin >> name; cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); student_lastnames.push_back(name); cout << "Student " << i + 1 <<" first name: "; cin >> name; cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); student_firstnames.push_back(name); cout << "Student " << i + 1 <<" score: "; cin >> score; cout << endl; cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); student_score.push_back(score); i++; } while ( i < numStudents); // sort them alphabetically sort (student_lastnames.begin(), student_lastnames.end()); for (int i =0; i < student_lastnames.size(); i++) { cout << student_lastnames[i] << ", " << student_firstnames[i] << ": " << student_score[i] << endl; } } void students::End() { cout << endl << endl << "Press any key to continue..."; // prompts the user to continue or quit char c = getch(); } // function main begins program exectuion int main() { students mystudent; mystudent.displayMessage(); mystudent.getData(); mystudent.End(); }
Comment