The program reads IDs and scores for a test and then outputs the ID, score, points based on score, and the grade. I'm getting all kinds of different errors, such as memory errors, DLL errors, and it says I'm using a certain char variable without it being initialized.
The first line in the file looks like: FFTFFTFFTTFTTFFTTFTF
Each line afterwards looks like: ABC56236 FFTFTFTTFTFTTFTTTFTF
#include <iostream>
#include <fstream>
usingnamespace std;
void process(char ID[], char score[], char answers[]);
int calcGrade(int pts);
int main()
{
char score[21];
char ID[9];
char trueAnswers[21];
char discard = ' ';
ifstream studentScores;
studentScores.open("ex6_studentScores.txt"); // this is only part of the entire file name, which is excluded here.
studentScores.get(trueAnswers, 20); // read the correct answers
studentScores.get(discard); // read the newline character
while (studentScores) // read and store info into ID and score and process and output the data.
{
studentScores.get(ID, 8); // read the student ID
studentScores.get(discard); // read the newline character after the student ID
studentScores.getline(score, 20); // read the student's scores
process(ID, score, trueAnswers);
}
studentScores.close();
system("pause");
return 0;
}
void process(char ID[], char score[], char answers[]) // calculate points based on correct and incorrect answers; output results
{
int points = 0;
for (int i = 0; i < 20; i++)
{
if (score[i] = ' ')
{
points += 0;
}
elseif (answers[i] == score[i])
{
points += 2;
}
else
{
points--;
}
}
if (points < 0)
{
points = 0;
}
cout << ID << " " << score << " " << points << " " << calcGrade(points);
cout << endl;
}
int calcGrade(int pts)
{
char grade; // this is the variable it says I can't use w/o initializing
switch (pts / 4)
{
case 1:
case 2:
case 3:
case 4:
case 5:
grade = 'F';
break;
case 6:
grade = 'D';
break;
case 7:
grade = 'C';
break;
case 8:
grade = 'B';
break;
case 9:
case 10:
grade = 'A';
break;
}
return grade;
}