// G. Hagopian PPP8 ex 7

#include <string>
#include <vector>
#include <iostream>
#include <algorithm> // std::swap

using std::cout;
using std::cin;
using std::vector;

/*7. 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
*/

void f(const int x) {
	int t = x;
	++t;
	cout << "\nx = " << x;
}

void print(const std::vector<std::string>& vs, const std::vector<double>& vd) {
	for (int i = 0; i < vs.size(); ++i)
		std::cout << "(" << vs[i] << ", " << vd[i] << ")" << std::endl;
}

void getNames(std::vector<std::string>& vs) {
	std::cout<< "\nEnter names.  To stop, enter \"stop\": ";
	std::string s;
	while (std::cin >> s && s != "stop")
		vs.push_back(s);
}

void getAges(const std::vector<std::string>& vs, std::vector<double>& vd) {
	double age{ 0. };
	for (std::string s : vs) {
		std::cout << "\nWhat is the age of " << s << "? ";
		std::cin >> age;
		vd.push_back(age);
	}
}

void fixAges(const std::vector<std::string>& sorted,
			 const std::vector<std::string>& orig,
			 std::vector<double>& ages) {
	std::vector<double> A = ages;
	for(int i = 0; i < sorted.size(); ++i) { //for each name in the sorted list
		//find its position in the original list
		int j = 0;
		while (sorted[i] != orig[j]) ++j;
		ages[i] = A[j];
	}
}
int main() {
	int y = 7;
	f(y);
	cin.get();
	std::vector<std::string> vs;
	std::vector<double> ages;
	getNames(vs);
	getAges(vs, ages);
	print(vs, ages);
	std::vector<std::string> names = vs;
	std::sort(vs.begin(), vs.end());
	fixAges(vs, names, ages);
	print(vs, ages);
	std::cin.ignore();
	std::cin.get();

}