/* Design and implement a Name_pairs class holding(name, age) 
pairs where name is a string and age is a double.
Represent that as a vector<string>(called name) and a
vector<double>(called age) member.
Provide an input operation read_names() that reads
a series of names.  Provide a read_ages() operation that prompts 
the user for an age for each name.  Provide a print() operation 
that prints out the (name[i], age[i]) pairs(one per line) in
the order determined by the name vector.  Provide a sort() 
operation that sorts the name vector in alphabetical order and 
reorganizes the age vector to match.  Implement all
“operations” as member functions.
Test the class (of course : test early and often).
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class Name_pairs {
private:
	std::string name;
	double age;
	std::vector<std::string> names;
	std::vector<double> ages;
public:
	void read_names();
	void read_ages();
	void print();
	void sortNamesAndAges();
};

void Name_pairs::read_names() {
	std::string s;
	std::cout << "Enter a sequence of names (\"Q\" to quit):\n";
	while (std::getline(std::cin, s) && s != "Q") {
		names.push_back(s);
	}
}

void Name_pairs::read_ages() {
	double age{ 0. };
	for (std::string s : names) {
		std::cout << "\nHow old is " << s << "?: ";
		std::cin >> age;
		ages.push_back(age);
	}
}

void Name_pairs::print() {
	for (int i = 0; i < names.size(); ++i)
		std::cout << '(' << names[i] << ", " << ages[i] << ")\n";
}

void Name_pairs::sortNamesAndAges() {
	//for (int i = names.size() - 1; i >= 0; --i) {
	for (int j = 0; j < names.size(); ++j) {
		for(int k = 0; k < names.size()-j-1; ++k)
			if (names[k][0] > names[k + 1][0]) { //if out of order
				std::swap(names[k], names[k + 1]);
				std::swap(ages[k], ages[k + 1]);
			}
	}
	//}
}
int main() {
	Name_pairs np;
	np.read_names();
	np.read_ages();
	np.print();
	np.sortNamesAndAges();
	std::cout << "Here they sorted: \n";
	np.print();
	std::cin.ignore();
	std::cin.get();
}

