The following is the problem description:
The history teacher at your school needs help in grading a True/False test.
The students’ IDs and test answers are stored in a file. The first entry in the
file contains answers to the test in the form:TFFTFFTTTTFFTFTFTFTT
Every other entry in the file is the student ID, followed by a blank, followed
by the student’s responses. For example, the entry:ABC54301 TFTFTFTT TFTFTFFTTFT
indicates that the student ID is ABC54301 and the answer to question 1 is
True, the answer to question 2 is False, and so on. This student did not
answer question 9. The exam has 20 questions, and the class has more than
150 students. Each correct answer is awarded two points, each wrong answer
gets one point deducted, and no answer gets zero points. Write a program
that processes the test data. The output should be the student’s ID, followed
by the answers, followed by the test score, followed by the test grade.
Assume the following grade scale: 90%–100%, A; 80%–89.99%, B;
70%–79.99%, C; 60%–69.99%, D; and 0%–59.99%, F.
This is the program I have made:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//declaring variables
ifstream file("grading.txt");
char key[21];
char id[9];
char student[21];
int score = 0, i;
//initializing arrays
for(i=0; i<21; i++)
{
key[i]=0;
student[i]=0;
}
for(i=0; i<9; i++)
id[i]=0;
//processing the key
file >> key;
file.ignore(100, "\n");
//processing student grades
while(file.good())
{
file >> id;
file.ignore();
getline(file, student);
file.ignore(100, "\n");
//comparing key and student answer
for(i=0; i<21; i++)
{
if(strcmp(student[i], key[i])
score += 2;
else
score -= 1;
}
//outputing student id, score and grade
cout << "Student ID: " << id;
cout << "Score: " << score;
score = (score/(40))*100;
if(score >= 90 && score <= 100)
cout << "Grade: A" << endl << endl;
else if(score >= 80 && score <= 89.99)
cout << "Grade: B" << endl << endl;
else if(score >= 70 && score <= 79.99)
cout << "Grade: C" << endl << endl;
else if(score >= 60 && score <= 69.99)
cout << "Grade: D" << endl << endl;
else if(score >= 0 && score <= 59.99)
cout << "Grade: F" << endl << endl;
else
cout << "Invalid percentage" << endl;
}
//closing file
file.close();
return 0;
}
I seem to be getting the following compilation errors: http://pastebin.com/r0Y1xX8M (couldn’t edit the errors properly on here, sorry)
Help would be appreciated with the compile errors, and any other suggestions as to how to solve this.
Your first problem is in
I believe that you want to use
'\n'with single quotes rather than double quotes. single quotes represents a character. double quotes represents an array or characters(a string)secondly, I believe that iostream contains a
stringclass, which encapsulates the character array. I believe thatgetlineuses that string class. For using character arrays, my personal preference is fscanf, but most of your code seems to be based around iostream, so changing your character arrays to strings might be the simplest change to make.also, your
if(strcmp(student[i], key[i])line is passing in characters instead of the character arrays. I believe thatif(student[i] == key[i])is what you want to do.your compilation errors tell you what line of code the error is on, so for the rest of them you can look at your code and think about the problem yourself.