Help with Quiz Grader

I am having trouble approaching this problem. I don't even know where to begin at the moment, I feel as if I know the logic but don't know how to put it into code.

A high school teacher, gave an online quiz to his class of 13 students that consisted of 10 multiple choice questions. The possible answers for each question were A, B, C, or D. You have been asked to write a C++ program to grade the quizzes.

The results of the quiz were saved in a text file named answers.txt. The first line of this file is the answer key, consisting of 10 characters, A, B, C, or D, separated by white space. After that there is one line for each student. Each student line starts with the student ID, which is at most 10 characters and contains no white space. After the student ID are the student's 10 answers, A, B, C, or D, separated by white space.


Output:
Your program should print one line of output for each student. This line consists of:

The student ID
The number of correct answers, a slash, and the total number of questions
An equal sign and the percent grade for the quiz
Spacing and justification should match the sample run below

Your program must read the input file into the following arrays before starting the grading process:

A single-dimensional array of type string that holds the student IDs
A single-dimensional array of type char that holds the answer key
A two-dimensional array of type char that holds the student answers
Once the arrays are populated, the input file should be closed and then the grades computed.

The answers.txt file would look like this:
A B B D C A B C D B
G.Hopper B A A A A B A A A A
S-Brin B A A A A B A A A B
Thompson A A A A A B A A A B
Bob.Jones A A B A C B B A A B
Liskov-JVM A A B A C B B C D B
J@Backup A B B B C A B C D B
EWDijkstra A B B D C A B C D B
--==<>==-- A B B D D A B C A B
0123456789 D C A D C A B C D A
X A B A D C A B C C B
abc4!!4cba D B B D C C B C D B
Zuse-First A C B D D A B C D B
CLRS A B A D C A D C D B


Any info is appreciated on how to approach this.
We generally don't do people's homework for them. You need to start writing your program and ask questions when you get stuck.

I recommend that you start with the following:

1. Open the file, read the first line, and write it back out to show that you can do it the way you expect. You can either read the whole line (getline) or read each answer character (extraction operator >>).

2. Create an array of characters for the answer key. Read the first line into the array. Print out the array to make sure you get what you expect.

3. Read the rest of the file line by line. Read the student ID and 10 answers. Print them out to make sure you get what you expect.

At that point you will have a pretty good idea of how to progress next.

Make sure you can do each step before you try the next. Programming is (or at least should be) an iterative process where little steps allow you to work out the bugs you introduce.
I understand.
I have the code to read the first line, and write it out. But I need to append that line to the answerKey array. I am not sure how to do this. the code i have to find the first line which is the answer key is:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
bool isWanted(const string & line);

int main(){
    
    string answerKey[10];
    char studentIds[10];
    char studentAnswers[10];
    string sLine;

    ifstream inFile("answers.txt");
    if(!inFile){
        cerr << "Error! cannot open the file";
        return EXIT_FAILURE;
    }

    while(getline(inFile, sLine)){
        if(isWanted(sLine)){
            cout << sLine << endl;
        }
    }
    return 0;
}

bool isWanted(const string & line){
    return line.find("A B B D C A B C D B") != string::npos;
}
Let me state it again. Make sure you can do each step before you try the next.

It looks like you are trying to do steps 2 and 3 without getting step 1 to work.

Step 1: For this step you don't need lines 10 - 12. The "isWanted" function is not needed at all, and is just screwing things up. In step 1 you only want to read 1 line, so the loop in line 21 is wrong. So, if you change lines 21 - 25 to a getline and a cout <<, you will have step 1 working.

Seriously, work is small steps and don't add stuff until you need it. My idea of step 1 is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>

int main(){
    std::string sLine;

    std::ifstream inFile("answers.txt");
    if(!inFile){
        std::cerr << "Error! cannot open the file";
        return EXIT_FAILURE;
    }

    getline(inFile, sLine);
    std::cout << sLine << std::endl;

    return 0;
}


Error checking on line 13 would be more robust, but as an initial step to make sure you can read a file it is not necessary.

Step 2: Now you need your line 10 (sort of). However, answerKey will be an array of chars, not an array of strings. You will be reading in an array of chars, so you probably want to use a loop. (Hint: arrays and loops almost always go together.) So, the simplest thing to do is to change your getline call to a loop that reads 10 characters from the file.

Oh, and please change the hard-coded '10's in your code to a constant of some sort.

const int numAnswers = 10;
Last edited on
I think it's helpful to copy/paste the assignment into your source code and turn it into comments. Then move the comments around and fill in the code as appropriate. For example:
Your program must read the input file into the following arrays before starting the grading process:

A single-dimensional array of type string that holds the student IDs
A single-dimensional array of type char that holds the answer key
A two-dimensional array of type char that holds the student answers
becomes
1
2
3
4
5
6
    // A single-dimensional array of type string that holds the student IDs
    string studentIDs[13];
    // A single-dimensional array of type char that holds the answer key
    char answers[10];
    // A two-dimensional array of type char that holds the student answers
    char studentAnswers[13][10];

and then, to avoid magic constants:
1
2
3
4
5
6
7
8
9
    const int NUM_STUDENTS=13;
    const int NUM_QUESTIONS=10;

    // A single-dimensional array of type string that holds the student IDs
    string studentIDs[NUM_STUDENTS];
    // A single-dimensional array of type char that holds the answer key
    char answers[NUM_QUESTIONS];
    // A two-dimensional array of type char that holds the student answers
    char studentAnswers[NUM_STUDENTS][NUM_QUESTIONS];


Next, use the same tedhnique to read the first line:
1
2
3
    // The first line of this file is the answer key, consisting of 10
    // characters, A, B, C, or D, separated by white space.
    // ADD YOUR CODE HERE TO READ THE FIRST LINE INTO THE answers ARRAY 

etc.
Topic archived. No new replies allowed.