Answer: This doesn't work fully, but it's a start. Good Luck
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
class People
{
private:
const static int SIZE = 30;
string names[SIZE];
int birth_years[SIZE];
int count;
void sort();
void display();
public:
People();
void simulate();
};
People::People()
{
count = 0;
// open both files
ifstream namesFile, birthyearsFile;
namesFile.open("Names.txt");
birthyearsFile.open("BirthYear.txt");
while (!namesFile.eof() && !birthyearsFile.eof() && count < SIZE)
{
getline(namesFile, names[count]);
birthyearsFile >> birth_years[count];
count++;
}
// files open failed, exit the program
if (namesFile.fail() || birthyearsFile.fail())
{
cout << "Unable to open input file(s). Terminating" << endl;
exit(1);
}
//close the files
namesFile.close();
birthyearsFile.close();
sort();
display();
}
void People::sort()
{
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - 1 - i; j++)
{
if (names[j] > names[j + 1])
{
string tempName = names[j];
names[j] = names[j + 1];
names[j + 1] = tempName;
int tempYear = birth_years[j];
birth_years[j] = birth_years[j + 1];
birth_years[j + 1] = tempYear;
}
}
}
}
void People::display()
{
cout << "Alphabetical Roster of Names: " << endl;
for (int i = 0; i < count; i++)
{
cout << names[i] << "\t" << birth_years[i] << endl;
}
cout << endl;
}
void People::simulate()
{
int year;
cout << endl << "Names by Birth Year" << endl;
// input the birth year
cout << "Please enter the birth year: ";
cin >> year;
// loop that continues until valid input has been read
while (cin.fail() || year < 1995 || year > 2005)
{
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid birth year entered, try again: ";
cin >> year;
}
bool found = false;
for (int i = 0; i < count; i++)
{
if (birth_years[i] == year)
{
if (!found)
{
cout << endl << "For the birth year of " << year << ":" << endl;
found = true;
}
// display the name
cout << names[i] << endl;
}
}
// no name with birth year found
if (!found)
cout << endl << "No names with the birth year " << year << "." << endl;
cout << "End of results" << endl;
}
int main()
{
People people;
people.simulate();
return 0;
}
Explanation: