//Read in name and year born from a file into a vector #include #include #include #include #include using namespace std; const int currentYear=2016; void getFile(vector &names,vector &yearBorn) { string input,yearString; ifstream people("c:/mycpp/names2.txt"); //input file stream if(people) { //file was opened while(getline(people,input)) { //Example: input="Jay 1994" int p=input.find_first_of(","); //Example: p=3 if(p>=0) { names.push_back(input.substr(0,p)); //Example: name="Jay" yearString=input.substr(p+1); // Example: yearString="1994" yearBorn.push_back(stoi(yearString)); //stoi=String To Int, Example yearBorn=1994 }//there was a comma }//each line people.close(); } }//getFile void sort(vector &names,vector &yearBorn) { int size=names.size(); for(int i=0;inames[j+1]) { //swap doesn't work with vectors, include algorithm and use iter_swap iter_swap(names.begin() + j, names.begin() +(j+1) ); iter_swap(yearBorn.begin() + j, yearBorn.begin() +(j+1) ); } //out of order } //inner loop }//outer loop }//sort void showAll(vector names,vector yearBorn) { cout<<"NAME\tAGE\n"; int count=names.size(); for(int i=0;i names; vector yearBorn; getFile(names,yearBorn); sort(names,yearBorn); showAll(names,yearBorn); system("pause"); return 0; } //main