我的课程的这一部分旨在阅读学生的姓名和成绩列表,然后将它们平均并显示出来。
我宣布了这样一个功能:
int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES],
string fileName, int maxStudents);
这是定义:
int loadStudentNamesGrades(string students[],
int grades[][MAX_GRADES],
string fileName,
int maxStudents)
{
ifstream inFile; // input file stream
string nameFile; // name of file
string studentName; // name of student
int numStudents = 0; // number of students initialized to 0
inFile.open(fileName); // open the file
if (!inFile)
{
cout << "Unable to Open File!\n";
system("PAUSE");
exit (EXIT_FAILURE);
}
for (int i = 0; i < maxStudents && (inFile >> studentName >> numStudents);
i++, numStudents++)
{
for (int j = 0; j < MAX_GRADES; j++)
{
inFile >> grades[i][j];
}
students[i] = studentName;
}
inFile.close();
return numStudents;
}
当我尝试运行程序时,我的菜单会显示,但文本文件中没有任何值填充。据我所知,我的文件正常打开,因为它不会返回错误。
答案 0 :(得分:0)
它看起来像读入数组,但你没有返回那些。尝试通过引用传递数组,如下所示:
int loadStudentNamesGrades(string (&students)[10], int (&grades)[10][MAX_GRADES],
string fileName, int maxStudents)
您还可以考虑使用vector
而不是数组。