Search code examples
c++arraysiochareof

Comparing two input files using only "char" in c++


I am looking for some help with an assignment question or more just a nudge in the right direction. We aren't allowed to use strings. We do need to use eof.

Question:

Two data files are required to evaluate a multiple-choice examination. The first file (booklet.dat) contains the correct answers. The total number of questions is 50. A sample file is given below: ACBAADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD

The second file (answer.dat) contains the students’ answers. Each line has one student record that contains the following information: The student’s answers (a total of 50 answers): each answer can be A, B, C, D or * (to represent no answer).

There are no blanks between answers. Student ID Student name A sample file is given below: AACCBDBCDBCBDAAABDBCBDBAABCBDDBABDBCDAABDCBDBDA 6555 MAHMUT CBBDBCBDBDBDBABABABBBBBABBABBBBDBBBCBBDBABBBDC** 6448 SINAN ACBADDBCBDDAACDBACCABDCABCCBDDABCACABABABCBDBAABD 6559 CAGIL

Write a C++ program that counts the total number of correct answers by each student and outputs this information to another file called report.dat.

For the sample files given above, the output should be as follows:

6555 MAHMUT 10

6448 SINAN 12

6550 CAGIL 49

Please see question and my code below. I am thinking it would be best to put the student answers into a 2d array but every time I try this, I don't get the correct output. Any help would be appreciated.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std; 


int main(){

char answerKey[50];
char studentDetails;
char studentAnswers[3][50];
char next;
ifstream memo, answers;

memo.open("booklet.dat");
if (memo.fail()){
    cout << "booklet.dat failed to open. \n";
    exit(1);
}

answers.open("answer.dat");
if (memo.fail()){
    cout << "answer.dat failed to open. \n";
    exit(1);
}

for (int i = 0; i < 50; i++){
    memo >> next;
    answerKey[i] = next;
    }

for (int i = 0; (next != '\n'); i++){
    for (int j = 0; j < 50; j++){
        answers >> next;
        studentAnswers[i][j] = next;
    }
}

return 0;
}

Solution

  • This is one method to accomplish your goal, there are many others.

    const unsigned int  MAX_ANSWERS = 50U;
    char answer_key[MAX_ANSWERS] = {0};
    
    // Read in the answer key.
    std::ifstream answer_file("booklet.dat");
    answer_file.read(&answer_key[0], MAX_ANSWERS);
    
    // Process the students answers
    char student_answers[MAX_ANSWERS] = {0};
    std::ifstream student_file("answer.dat");
    while (student_file.read(&student_answers[0], MAX_ANSWERS))
    {
        correct_answers = 0;
        for (unsigned int i = 0; i < [MAX_ANSWERS]; ++i)
        {
            if (student_answers[i] == answer_key[i])
            {
                ++correct_answers;
            }
        }
        // Output the remainder of the line.
        char c;
        while (student_file >> c)
        {
            if (c == '\r')  continue; // Don't print the CR
            if (c == '\n')
            {
                 cout << correct_answers;
                 cout << endl;
                 student_file.ignore(10000, '\n');
                 break;
            }
            cout << c;
        }
    }
    

    In the code above, the answer key is read and stored.

    For each student line, the student's answers are read in, then compared to the answer key. The inner loop counts the number of correct answers.

    After the answers are compared, the remaining part of the data line is printed, until the end of the line. When the end of the line is encountered, the correct answer quantity is printed.