fstream functions

Hello, I'm having trouble understanding fstream functions and I have to use it to complete this program. I have to read information from a txt file:
Martin 22
Austin 18
Daniel 19
Marggie 23
Tyler 16
then output the names and ages in a table, determine and display the names of teenagers (13-19), take the average of the ages, and output the names of those above average. I think all of my functions are correct except for the ReadData one. Some help and clarification on fstream functions would be appreciated!

#include <iostream>
#include<fstream>
#include <string>
using namespace std;
void ReadData(string name[], int age[]);
void DisplayData(string name[], int age[]);
void DisplayTeen (string name[], int age []);
float FindAve (int age[]);
void DisplayAboveAve( string name[], int age[]);

int main()
{
//Declares identifiers
const int N=5;
string Name[N];
int Age[N];

//Reads data from text file
ReadData (Name, Age);
//Outputs data from text file
cout<<"This is the name and age of all students";
DisplayData (Name, Age);
cout<<endl;

//Displays the names of teenagers
cout<<"This is the name of all teenagers.";
DisplayTeen (Name, Age);
cout<<endl;

//Finds average of ages
cout<<"The average of all students is ";
float Average=FindAve(Age);
cout<<Average;

//Displays the names of students with ages above average
cout<<"This is the name of all students whose age is above average";
DisplayAboveAve (Name, Age);
cout<<endl;

return 0;
}

//----------------------------------------------------------------------
//Name ReadData
//Purpose Reads data into file Name.txt
//----------------------------------------------------------------------
void ReadData(string name[], int age[])
{
ifstream F;
F.open("Name.txt");
for (int n=0; n<5; ++n)
{
F>> name[n];
F>> age[n];
}
F.close();
}

//----------------------------------------------------------------------
//Name DisplayData
//Purpose Displays data from file Name.txt
//----------------------------------------------------------------------
void DisplayData(string name[], int age[])
{
for (int n=0; n<5; ++n)
{
cout<< name[n];
cout<< age[n];
}
}

//----------------------------------------------------------------------
//Name DisplayTeen
//Purpose Dispays the names of teenagers
//----------------------------------------------------------------------
void DisplayTeen (string name[], int age[])
{
for ( int n=0; n<5; ++n)
{
if ( age[n]>=13 || age[n]<= 19)
cout<< name[n];
}
}

//----------------------------------------------------------------------
//Name FindAve
//Purpose Finds average of the ages
//----------------------------------------------------------------------
float FindAve (int age[])
{
float average;
int total=0;

for (int n=0; n<5; ++n)
{
total+= age[n];
}

return average= total/5.0;
}

//----------------------------------------------------------------------
//Name DisplayAboveAve
//Purpose Displays the name of students whose age is above average
//----------------------------------------------------------------------
void DisplayAboveAve( string name[], int age[], float Average)
{
for (int n=0; n<5; ++n)
{
if ( age[n]>=Average)
cout<<name[n];
}
}
Last edited on
Topic archived. No new replies allowed.