/******************************************************************************************
ex6.cpp
Print the records based on criteria: younger than input
******************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// class definition
class person {
public:
string first;
string last;
int age;
};
// prototypes
void showYoungerThan(person [], const int size, const int age);
void readFile(string filename, person [], const int maximum, int& size);
void showPeople(person [], const int size);
// MAIN!
void main() {
int count=0;
const int maxsize=100;
person people[maxsize];
string filename;
cout << "Enter the file to read: ";
cin >> filename;
readFile(filename, people, maxsize, count);
int limit;
cout << "Enter the upper age limit: ";
cin >> limit;
showYoungerThan(people, count, limit);
}
//------- Print the people in the list younger than the supplied age ------
void showYoungerThan(person people[], const int size, const int age) {
cout << "People below age " << age << ":\n";
for (int i=0; i < size; i++) {
if (people[i].age < age)
cout << '\t' << people[i].last << ", " << people[i].first
<< " (" << people[i].age << ")\n";
}
}
//------- display the whole list ------
void showPeople(person people[], const int size) {
for (int i=0; i < size; i++) {
cout << people[i].last << ", " << people[i].first << " (" << people[i].age << ")\n";
}
}
//------- fill each person in the list from a file ------
void readFile(string filename, person people[], const int maximum, int& size) {
ifstream fin(filename.c_str());
for (int i=0; i < maximum; i++) {
if(fin >> people[i].last >> people[i].first >> people[i].age)
size++;
}
}