Instructions: 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
My issue is that I can get the first two right, but then the next two grades are a few digits off. Any Help?
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
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(){
char studentAnswer[20];
char studentResponses;
int totalScore;
string id;
ifstream inFile;
inFile.open("Ch8_Ex6Data.txt");
for(int i = 0; i < 20; i++){
inFile >> studentAnswer[i];
}
while(inFile >> id){
cout << id << " ";
inFile.get(studentResponses);
totalScore = 0;
for(int i = 0; i < 20; i++){
inFile.get(studentResponses);
cout << "" << studentResponses;
if(studentResponses == ' '){
totalScore+=0;
}
else if(studentResponses == studentAnswer[i]){
totalScore+=2;
}
else{
totalScore-=1;
}
}
cout << " " << totalScore << " ";
double a = totalScore * 2.5;
if(a >= 90){
cout << 'A' << endl;
}
else if(a >= 80){
cout << 'B' << endl;
}
else if(a >= 70){
cout << 'C' << endl;
}
else if(a >= 60){
cout << 'D' << endl;
}
else{
cout << 'F' << endl;
}
}
return 0;
}
|
Ch8_Ex6Data.txt:
1 2 3 4 5
|
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
|