#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Record
{
string name;
int age;
};
string getFileName()
{
//makes a copy
string filename;
cout << "Enter Filename: ";
cin >> filename;
return filename;
}
void readFile(string filename, Record array[])
{
//open it
ifstream fin(filename.c_str());
//test it
if (fin.fail())
{
return -1;
}
//read it
int count = 0;
while(fin >> array[count].name)
{
fin >> array[count].age;
count++;
}
//close it
fin.close();
//return it
return count;
}
void display(Record array[], int size)
{
for (int i = 0; i < size; i++)
{
cout << i << ": " << array[i].name << ", " << array[i].age << endl;
}
}
int main()
{
//get the file from user
string filename = getFileName();
//read the file
Record array[500];
int count = readFile(filename, array);
//display it on the screen
display(array, count);
return 0;
}