This is the exercise spec:
The part I'm having difficulty with, mainly, is the Hint part ("Hint: Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name."). I'm not sure how to actually do that.Read five names into a vector<string> name, then prompt the user for the ages of the people named and store the ages in a vector<double> age. Then print out the five (name[i],age[i]) pairs. Sort the names (sort(name.begin(),name.end())) and print out the (name[i],age[i]) pairs. The tricky part here is to get the age vector in the correct order to match the sorted name vector. Hint: Before sorting name, take a copy and use that to make a copy of age in the right order after sorting name. Then, do that exercise again but allowing an arbitrary number of names.
This the code I've got:
Code:#include <iostream> #include <vector> #include <algorithm> #include "../custom_std_lib_facilities.h" using namespace std; int main() { vector<string> names; vector<int> ages; string name; int age; cout << "Please enter five names: "; for (int i = 0; i < 5; i++) { cin >> name; cin.ignore(); names.push_back(name); sort(names.begin(), names.end()); cout << "This was name number " << i + 1 << "\n"; } cout << "Please enter those five people's ages: "; for (int i = 0; i < 5; i++) { cin >> age; cin.ignore(); ages.push_back(age); cout << "This was age number " << i + 1 << "\n"; } for (unsigned i = 0; i < names.size(); i++) { cout << names[i] << " is of age " << ages[i] << "\n"; } keep_window_open(); }