name age pairs 1.0

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 8 Exercise 7
Using std_lib_facilities.h by Bjarne Stroustrup.

main.cpp main file

[code language=”cpp”]
//Philipp Siedler
//Bjarne Stroustrup’s PP
//Chapter 8 Exercise 7

#include "std_lib_facilities.h"

vector<string> names;
vector<int> ages;

class NameAge {
vector<string> n;
vector<int> a;
public:
NameAge(vector<string>& _n, vector<int>& _a) :n(_n), a(_a) {};
void readNames();
void readAges();
void print();
void sortVectors();
};

void NameAge::readNames() {
while (cin) {
char letter;
cin.get(letter);
if (isalpha(letter)) {
string name;
name += letter;
while (cin.get(letter) && isalpha(letter)) {
name += letter;
}
n.push_back(name);
}
if (isspace(letter)) {
if (letter == ‘\n’) {
break;
}
}
}
}

void NameAge::readAges() {

while (n.size() != a.size()) {
char ageChar;
int ageInt;
cin.get(ageChar);
if (isspace(ageChar)) {
if (ageChar == ‘\n’ && n.size() == a.size()) {
break;
}
}
cin.unget();
cin >> ageInt;
a.push_back(ageInt);
}
}

void NameAge::print() {
for (int i = 0; i < n.size(); i++) {
cout << n[i] << " " << a[i] << "\n";
}
}

void NameAge::sortVectors() {
vector<string> tempNames = n;
vector<int> tempAges = a;
vector<int> index;

sort(tempNames.begin(), tempNames.end());

for (int i = 0; i < n.size(); i++) {
for (int j = 0; j < n.size(); j++) {
if (tempNames[i] == n[j]) {
index.push_back(j);
}
}
}

for (int i = 0; i < n.size(); i++) {
a[i] = tempAges[index[i]];
}
n = tempNames;
}

int main()
try
{
NameAge na(names, ages);

cout << "Enter name(s)\n";

na.readNames();

cout << "Promt ages in order of names:\n";

na.readAges();

na.sortVectors();

na.print();

keep_window_open();
}

catch (runtime_error e) {
cout << e.what() << "\n";
keep_window_open();
}

catch (…) {
cout << "Exiting\n";
keep_window_open();

}
[/code]

Output:
Enter name(s)
Philipp Alfred Isabell Benjamin
Promt ages in order of names:
28 60 31 23
Alfred 60
Benjamin 23
Isabell 31
Philipp 28
Please enter a character to exit

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124