Reading From Data Files using structures and functions

My main question is how to solve where I am going wrong with my code, I am a moderately skilled programmer and I was assigned this code for one of my projects. The problem starts in the getData function after reading through the if statement.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Person
{
float avg;
string name;
int age;
};
void GetData(ifstream& infile, string name[], int age[], float ave[])
{

if (infile.is_open())
{
int i = 0;
infile >> name[5] >> age[5];
for (int j = 0; j < 5; j++)
infile >> ave[i];
i++;
}
else
cout << "The file could not be opened" << endl;

infile.close();
}
void findTeens(int age[], string name[])
{
if (age[5] <= 19)
{
cout << name[5];
}

}
void findAvgAge(int age[])
{
int c = age[5] / 5;
cout << c;
}
void findAvgScore(float ave[])
{
int s = ave[5] / 5;
cout << s;
}
int main()
{
/*
const int n = 5;
Person a[n];
a[n].age;
a[n].avg;
a[n].name = name;
*/
float ave[5];
string name[5];
int age[5];

ifstream infile("data.txt");

if (!infile)
{
cout << "Cannot run program please try again" << endl;
}

GetData(infile, name, age, ave);
cout << name[5] << age[5] << ave[5];

cout << "The teenagers are:" << endl;
findTeens(age, name);

cout << "The average afe of the class is" << endl;
findAvgAge(age);
cout << "The average score of the class is" << endl;
findAvgScore(ave);

system("pause");
}
Last edited on
This is what you do:
1
2
int age[5];
age[5] = 42;

What is wrong in the index that you use?
Topic archived. No new replies allowed.