So my program is supposed to read in a file in the format name score and store in an array of no more than 12.
Tom 30
Jill 87
Scott 98
My problem occurs when I try to print the names from the data file that contains a higher number of data than will fit in the array. Any data file with 12 pieces of data or less will work. The score array will print fine for the data file greater than 12. I get the right amount of data read (i.e 12), but when i try to print the names i get a seg fault. Any help is appreciated.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
constint maxsize = 12;
int max = 0;
int min = 100;
int above = 0;
float sum = 0.0;
float average = 0.0;
string name [12];
int score [12];
string filename, inputfilename;
ifstream myIn;
cout << "What data file do you want to use? ";
getline (cin, inputfilename);
myIn.open(inputfilename.c_str());
int i = 0;
myIn >> name[i] >> score[i];
int count = 0;
while (myIn && i < maxsize)
{
if(score[i] > max)
max = score[i];
if(score[i] < min)
min = score [i];
if(score[i] > 80)
above++;
sum +=score[i];
count++;
i++;
myIn >> name[i] >> score[i];
}
cout << "There were " << count << " students read in from the file." << endl << endl;
cout << "The names of those students:" << endl;
for(int j=0;j<count;j++){
cout << name[j] << " ";
}
cout << endl << endl;
cout << "The grades of those students: " << endl;
for(int k=0;k<count;k++){
cout << score[k] << " ";
}
cout << endl << endl;
average = sum/count;
cout << "The avg grade of those students is: " << fixed << setprecision(2) << average << endl;
cout << "The max grade is: " << max << endl;
cout << "The min grade is: " << min << endl;
cout << "The number of students with a grade over 80 points is: " << above << endl;
myIn.close();
return 0;
}