/******************************************************************************************
ex7.cpp
extended OOP object in a "traditional" array, input from a file and find the youngest
******************************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// class definition
class name {
public:
string first;
string last;
int age;
};
// prototypes
void readFile(string filename, name array[], int size);
int indexOfYoungest(name array[], int size);
// MAIN!
void main() {
int i;
const int maxsize=6;
name family[maxsize];
string filename;
cout << "Enter the file to read: ";
cin >> filename;
readFile(filename, family, maxsize);
int youngest = indexOfYoungest(family, maxsize);
cout << "The youngest is " << family[youngest].first << ", who is " <<
family[youngest].age << " years old.";
return;
}
//------- fill each person in the list from a file ------
void readFile(string filename, name array[], int size) {
ifstream fin(filename.c_str);
for (i=0; i < size; i++) {
fin >> family[i].first >> family[i].last >> family[i].age;
}
return;
}
//------- Find the youngest person in the list ------
int indexOfYoungest(name array[], int size) {
int youngestAge = array[0].age;
int youngestPos = 0;
for (int i = 1; i < size; i++)
if (array[i].age < youngestAge) {
youngestAge = array[i].age;
youngestPos = i;
}
return youngestPos;
}