tags:

views:

191

answers:

1

I have a homework program that I need a little help on. I need to compare an array containing the answer key of a test to an array containing the student answers. The problem I am running into is that I need to take the blank answers into account. I cannot seem to come up with code that will compare the arrays, then display the score. The test is scored as 2 points for a correct answer, minus 1 point for an incorrect answer, and zero points for a blank answer.

This is an example of the input:

TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF

The first line is the key, the second line is the first line of student data.

This is the code that I have:

#include <cmath>
#include <fstream>
#include <cstring>
#include <string>
#include <iostream>

using namespace std;

int checkAnswers(char key[], char answers[]);
void displayGrade(int score);

int main()
{
    ifstream inFile;

    int score = 0;
    char key[21];
    string studentID;
    char answers[21];
    int studentCount;

    inFile.open("Ch9_Ex6Data.txt");  //opens input file
    if (!inFile)  //sets condition for if input file does not exist
    {
        cout << "Unable to locate file." << endl;  //informs user that input file is missing
        cout << "Program terminating." << endl;  //informs user that program is terminating
        return 1;  //terminates program with error
    }

    inFile.getline(key, 21);

    cout << "Processing Data..." << endl << endl;
    cout << "Key: " << key << endl << endl;

    while (inFile >> studentID)
    {
        cout << studentID;
        inFile.getline(answers, 22);
        cout << answers << " ";
        score = checkAnswers(key, answers);  //calls checkAnswer function and sets result equal to score
        displayGrade(score);

    }

    return 0;
}

 //User-defined Function 1
int checkAnswers(char key[], char answers[])
{
        //Function Variables
    int i, length;  //declares i variable
    int correct = 0, incorrect = 0, blank = 0, score = 0;  //declares and initializes correct, incorrect, blank, and score variables

    answers >> length;
    for (i = 0; i < 22; i++)  //initiates conditions for for loop
    {
        if (answers[i] == ' ')  //initiates if condition
        {
            i++;
        }
        else if (key[i] == answers[i])  //initiates if condition
        {
            correct++;  //sets condition for correct answers
        }

        else if (key[i] != answers[i])  //initiates if condition
        {
            incorrect++;  //sets condition for incorrect answers
        }

        score = 40 - incorrect;  //calculates score
    }

    cout << score << " ";  //output student score
    return score;  //pass score
}

Edit for clarification: I need the code to display like so:

Key: TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF 27 D
ADE62366 TTFTFTTTFTFTFFTTF__ 34 B (with the _ being spaces)

The way it is displaying is like this:

Key: TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF 27 D
ADE62366 TTFTFTTTFTFTFFTTF 34 B

Which, I think, is an alignment issue, since I have tweeted the code some now.

+2  A: 

Some remarks:

    char answers[21];
    inFile.getline(answers, 22);

You cannot read 22 characters into a 21-sized array.

answers >> length;

This doesn't make any sense.

for (i = 0; i < 22; i++)  //initiates conditions for for loop

Why do you loop to index 21 if you only have 20 answers?

    score = 40 - incorrect;  //calculates score

This can be placed after the loop, but why doesn't calculate the score according to your rules (2*correct-incorrect)?

jpalecek