Hi,
My assignment is to read in data from a file(first name, last name, and grade) and then output it. i have my code and i beleive the problem lies within the function readData, but i cant figure out what it is. I run the program and i get an error saying it must be shut down, however i also get the "press any key to continue" in the box. i end up with a blank output txt file. does anyone have any idea?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int numStudents = 20;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
void readData(ifstream&, studentType person[]);
void assignGrade(studentType person[]);
int findHighest(studentType person[]);
void print(ofstream&, studentType person[]);
int main()
{
ifstream inFile;
ofstream outFile;
int students = 20;
inFile.open("c:\\chap11.txt");
if (!inFile)
{
cout<<"not working";
return 1;
}
outFile.open("c:\\chap11output.txt");
studentType person[numStudents];
readData(inFile, person);
assignGrade(person);
findHighest(person);
print(outFile, person);
inFile.close();
outFile.close();
return 0;
}
void readData(ifstream& in, studentType person[numStudents])
{
for (int index=0; index < numStudents; index++)
{
in>>person[numStudents].studentFName>>person[numStudents].studentLName>>
person[numStudents].testScore;
}
}
void assignGrade(studentType person[numStudents])
{
for (int y = 0; y < numStudents; y++)
{
if (person[numStudents].testScore > 90)
person[numStudents].grade = 'A';
else
if ((person[numStudents].testScore > 80) && (person[numStudents].testScore < 90))
person[numStudents].grade = 'B';
else
if ((person[numStudents].testScore > 70) && (person[numStudents].testScore < 80))
person[numStudents].grade = 'C';
else
if (person[numStudents].testScore > 60 && person[numStudents].testScore < 70)
person[numStudents].grade = 'D';
else
person[numStudents].grade = 'F';
}
}
int findHighest(studentType person[])
{
int max = 0;
for (int r = 1; r < numStudents; r++)
if (person[r].testScore < person[max].testScore)
max = r;
return max;
}
void print(ofstream& out, studentType person[numStudents])
{
for (int h=0; h < numStudents; h++)
out<<person[numStudents].studentLName<<","<<person[numStudents].studentFName<<" "<<person[numStudents].grade;
}