Code:
#include "std_lib_facilities.h"
class name_pairs
{
public:
double age;
string name;
name_pairs() // constructor
{
string name; // empty string
double age = double {}; // zero
vector <string> names_vec = vector <string>{};
vector <double> ages_vec = vector <double>{};
}
void read_ages(&name, n) // member function
{
double age;
names_vec.push_back(n);
cout << "Please enter an age for " << n << endl;
cin >> age;
ages_vec.push_back(age);
}
private:
vector <string> names_vec;
vector <double> ages_vec;
};
// driver program
int main()
{
name_pairs object;
cout << "Input a name - 'e' to exit" << endl;
cin >> object.name;
while(object.name != 'e')
{
cout << "Input an age: " << endl;
cin >> object.age;
object.read_ages(object.age);
bool response = 0; // false
cout << "Is there another name you wish to input? 1 for y; 0 for n " << endl;
cin >> response;
if (response == 1) //
{
cout << "Input a name - 'e' to exit:";
cin >> object.name;
}
else // response is false
{
cout << "Ending input now" << endl;
object.name = 'e';
}
}
return 0;
}
This should be simple. Im reading Programming Principles & Practice, chapter 9, exercise 2:
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> cales 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 (Not yet implemented) that prints out the name[i].age[i] pairs , one per line. in the porder determined by the name vector. provide a sort() operation - not yet implemented...
Sadly the above code is what I have attempted to put together but it does not even compile. Can anyone help me?