I am trying to calculate students grades from a file. When entering the if statement the debugger skips all my functions and ends the program. I think it has something to do with the file not being inputted well.
int main()
{
string temp, out = "";
ifstream RF("TestAnswers(5).txt");
while (getline(RF, temp))
{
out += temp;
}
GradeCalc gCalc;
gCalc.gradeAspects(out);
gCalc.gradeCalculation();
gCalc.grade();
}
class GradeCalc
{
private:
#define DELIM ','
struct GradeAspects
{
string studentN;
string answers;
string firstname, lastname;
double scores;
int rsize;
string percent;
string readFile; // int read;
};
vector<GradeAspects> read;
GradeAspects readFile[25];
public:
void gradeAspects(string filename);
void gradeCalculation();
void grade();
};
void GradeCalc::gradeAspects(string filename)
{
ifstream input;
string line;
int rnum = 0;
char delim;
int rsize;
input.open(filename);
if (input)
{
while (!input.eof())
{
getline(input, line);
delim = line.find(DELIM);
readFile[rnum].studentN = line.substr(0, delim);
line = line.substr(delim + 1, line.length() - 1);
delim = line.find(DELIM); // moving on to firstname by finding delim.
readFile[rnum].firstname = line.substr(0, delim);
line = line.substr(delim + 1, line.length() - 1);
delim = line.find(DELIM); // moving on to lastname by finding delim.
readFile[rnum].lastname = line.substr(0, delim); // last name
line = line.substr(delim + 1, line.length() - 1);
delim = line.find(DELIM);
readFile[rnum].answers = line; // line = line.substr(delim + 1, line.length() - 1);
rnum++;
}
}
rsize = rnum;
}
void GradeCalc::gradeCalculation()
{ // once done with reads move to calculations of how many answers were right etc.
int rnum = 0;
int rsize = 0;
string answers = readFile[0].answers;
int aSize = answers.size();
for (int i = 0; i < rsize; i++)
{
int score = 0;
for (int t = 0; t < aSize; t++)
{
if (answers[t] == readFile[i].answers[i])
score++;
{
answers[i] = score;
}
}
}
}
void GradeCalc::grade()
{
string line;
int rnum = 0;
int aSize = line.length();
int choices{};
int rsize = 0;
int grade{};
for (int i = 0; i < rsize; i++)
{
int score = 0;
readFile[i].answers = choices;
readFile[i].percent = grade;
readFile[i].percent = (choices / aSize * 100);
if (grade >= 90)
readFile[rnum].firstname = "A";
else if (grade > 80 && grade < 90)
readFile[rnum].firstname = "B";
else if (grade > 70 && grade < 80)
readFile[rnum].firstname = "C";
else if (grade < 70)
readFile[rnum].firstname = "F";
cout << "student" << readFile[rnum].firstname << " " << readFile[rnum].lastname << " had "
<< readFile[i].answers << " out of " << aSize << "and has a grade of" << grade << endl;
}
}