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.

SO this is what my input file looks like
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
and here is my code
Can someone plz help me find my mistake? Its not working. All i get for my output is "The correct answers are"
#include <iomanip>
#include <cmath>
#include <fstream>
#include<string>
#include<iostream>
using namespace std;

int check_correct_answers ( char answerKey[], char studentAnswers[], char grade, double score);
char print_student_grade( int score);
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open ("input");

double score;
char grade;

string header;
string studentID;
string studentAnswers;
getline(inFile,header);
cout<<"The correct answers are "<< header << endl<<endl;
while(getline(inFile, studentID))
{
cout << studentID <<" ";
getline(inFile, studentAnswers);
cout << studentAnswers << endl;
}
return 0;
}
int check_correct_answers( char answerKey[], char studentAnswers[], int score, char grade)
{
int i, correct = 0, incorrect = 0, blank = 0;
for (i = 0; i < 23 - 1; i++)
{
if (answerKey[i] == studentAnswers[i])
correct++;
if (answerKey[i] != studentAnswers[i])
incorrect++;
if (studentAnswers[i] == ' ')
{
blank++;
incorrect--;
}
score = (correct * 5) + (incorrect * -2) + (blank * -4);
}
cout << score << endl;
return score;
}

char print_student_grade( int score )
{
int i;
for (i = 0; i < 30; i++)
{
if (score >= 90)
cout<<"A"<<endl;
else if (score < 90 && score > 79)
cout<< "B"<<endl;
else if (score <= 79 && score > 69)
cout<< "C"<<endl;
else if (score <= 69 && score > 60)
cout<< "D"<<endl;
else if (score <= 59)
cout<< "F"<<endl;
}

return 0;
}
getline - true to it's name will return an entire line from the input stream. Giving the string a suggestive name means nothing to getline as long as you have not specified another delimiter.

To specify a different delimiter other than the newline character, you can do:
1
2
3
4
5
6
while(getline(inFile, studentID, ' '))
{
    cout << studentID <<" ";
    getline(inFile, studentAnswers);
    cout << studentAnswers << endl;
}
I am still getting only "The real answers are"
Can someone else try to compile this? I am not sure if it my computers fault
Not sure what else you are doing. The code compiles fine (with a few warnings) and when I run it, I get more than just "The correct answers are"

Link:
http://coliru.stacked-crooked.com/a/9c24b422482f23b8
Last edited on
Topic archived. No new replies allowed.