I'm working on a project where I need to read a .txt file that consists of a name(first and last, or just first) and 8 grades delimited by a comma (6 project grades, 2 exam grades), with up to 20 students. We just started learning about structs so I'm still trying to figure them out. The text file is formatted as such:
John Doe,95,84,67,78,85,92,100,76
Susan,83,56... etc..
I thought this program would be a breeze but I've been stuck for hours trying to figure why it won't work. I can get the name to read into the array using the getline function, but I can't figure out how to read in the grades. Here's what I have right now, not working of course, but just to show what I'm trying to do.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
const int SIZE = 20;
const int gradeSize = 4;
fstream gradeFile;
string fileName;
struct studentInfo
{
char studentName[SIZE];
char letterGade[gradeSize]; //not used at this point
int projectOneGrade[gradeSize];
int projectTwoGrade[gradeSize];
int projectThreeGrade[gradeSize];
int projectFourGrade[gradeSize];
int projectFiveGrade[gradeSize];
int projectSixGrade[gradeSize];
int examOneGrade[gradeSize];
int examTwoGrade[gradeSize];
double finalWeightedAvg; //not used at this point
};
// prototypes
void ProcessTextFile(string&, studentInfo[]);
bool OpenFile (fstream&, string); //simply opens input file
int main ()
{
studentInfo student[SIZE];
ProcessTextFile(fileName, student);
return 0;
}
void ProcessTextFile(string& fileName, studentInfo student[])
{
int i = 0;
// no issues with this function, so I left out the definition
OpenFile(gradeFile, GRADE_FILE);
while(!gradeFile.eof())
{
for(i = 0; i < SIZE; i++)
{
// this reads in fine
gradeFile.getline(student[i].studentName,SIZE, ',');
/* these are what I'm having trouble with, how can I read each
grade into each member?
*/
gradeFile >> student[i].projectOneGrade;
gradeFile >> student[i].projectTwoGrade;
gradeFile >> student[i].projectThreeGrade;
gradeFile >> student[i].projectFourGrade;
gradeFile >> student[i].projectFiveGrade;
gradeFile >> student[i].projectSixGrade;
gradeFile >> student[i].examOneGrade;
gradeFile >> student[i].examTwoGrade;
}
}
}
|
The grades have to be integer arrays. I've tried different methods such as reading the grade into an int variable, and then trying to set the array equal to that, like student[i].projectOneGrade = grade1; but I got an error saying
"cannot convert from 'int' to 'int [4]'." Perhaps I'm skipping a step? I'm at a complete loss, any help would be welcome!