CODE
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// readData returns theStream, aNAme, aBDay, and anAge
void readData(ifstream& theStream, string& aName, string& aBDay, int& anAge);
// findAges pass in an int array of ages and int numInArray -
// returns leastAgeIndex and greatestAgeIndex
void findAges(int agesArray[], int numInArray, int& leastAgeIndex, int& greatestAgeIndex);
//create method to output the persons with the least and greatest age
void outputData(string namesArray[], string bdayArray[], int ageArray[], int leastAgeindex, int greatesAgeIndex);
int main(){
// open the input file
ifstream inStream;
inStream.open("c:\\inputfile.txt");
//declare variables to read from input file and declare arrays
string name;
string namesArray[100];
string bday;
string bdayArray[100];
int age;
int ageArray[100];
// declare a counter to help loop through the array and keep track of number of elements
// initialize to 0 to start at first element of the array
int counter = 0;
// deaclare variable to keep track of the index in arrays for least and greatest age
int leastAgeIndex;
int greatestAgeIndex;
while (! inStream.eof())
{
// call the readData Function - it returns name, age, bday
readData(inStream,name,bday,age);
// store name, age, bday in the correct element of the array
namesArray[counter] = name;
bdayArray[counter] = bday;
ageArray[counter] = age;
// increment counter
counter = counter +1;
}
// close input stream - we are finish reading
inStream.close();
// call findAges - returns leastAgeIndex and greatestAgeIndex
findAges(ageArray,counter, leastAgeIndex,greatestAgeIndex);
//output - to check that it is correct
//cout << leastAgeIndex <<" " << greatestAgeIndex<< endl;
outputData(namesArray,bdayArray,ageArray,leastAgeIndex,greatestAgeIndex);
}
void readData(ifstream& theStream, string& aName, string& aBDay, int& anAge)
{
theStream >>aName >>aBDay >>anAge;
}
void findAges(int agesArray[], int numInArray, int& leastAgeIndex, int& greatestAgeIndex)
{
int lowestAge = 1000;
int greatestAge = 0;
for (int i = 0; i < numInArray; i=i+1)
{
if (agesArray[i] < lowestAge)
{
leastAgeIndex = i;
lowestAge = agesArray[i];
}
if (agesArray[i] > greatestAge)
{
greatestAgeIndex = i;
greatestAge = agesArray[i];
}
}
}
void outputData(string namesArray[], string bdayArray[], int ageArray[], int leastAgeIndex, int greatestAgeIndex)
{
cout<<"The person with the least age is " << namesArray[leastAgeIndex]<<endl;
cout<<"The person with the greatest age is " << namesArray[greatestAgeIndex]<<endl;
}
This post has been edited by jayman9: 15 Dec, 2007 - 02:30 PM