Test Grading Program

This program is supposed to take in test answers from the user and compare them to a question bank. At the end, the program should output what exact questions were incorrect, how many total correct answers there are, and whether or not they passed.

The code compiles fine but does not give out the right answer. When I input for each question, it tells me that some are correct that are incorrect, and vice versa.

Is there a problem in storing "R" or "W" (for right or wrong) as a char and then comparing that? Would a bool be more effective? Also, is storing the correct answers in sum the best way to do that?

There is just a lot going on in this code and I'm having a hard time identifying the problem. Also, this is a portion of a homework assignment so general suggestions rather than super specific corrections would be appreciated. Thanks for 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
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int SIZE = 20;
    //characters for loop that tests if A B C or D
    char A, B, C, D;
    //correct answer key
    char correctAnswers[SIZE] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
    //array for user answers
    char userAnswers[SIZE];
    //array to store if a question is right (R) or wrong (W)
    char Correctness[SIZE];
    int sum = 0;
    int index, count; //counter variables
    int finalcount;

    cout << "Driver's License Exam Grader" << endl;
    //for loop takes in user selection
    for (index = 1; index < 21; index++)
    {
        cout << "Enter your answer for question " << index << endl;
        cin >> userAnswers[index];

        //if loop to compare users answers and answer key
        if (userAnswers[index] != correctAnswers[index])
        {
            //if user enters incorrect answer, store W
            Correctness[index] = 'W';
        }
        else
        {
            //if user answer is correct, store R and and 1 to sum.
            Correctness[index] = 'R';
        }
    }
    //this for loop is to read the "correctness" array and output Right or Wrong accordingly
   for (index = 1; index < 21; index++)
    {
        //if correctness is W, meaning is it Wrong, output that it is incorrect
        //no output here if correct
        if (Correctness[index] == 'W')
        {
            cout << "Answer " << index << " is incorrect." << endl;
        }
        else
        {
            sum += 1;
        }
    }

    finalcount = 20 - sum;
    //output final results
    cout << "Results (minimum 15 correct to pass):" << endl;
    cout << "You got " << sum << " correct. " << endl;
    cout << "You missed " << (20 - sum) << "." << endl;
    if (sum >= 15)
    {
        cout << "You've passed!" << endl;
    }
    else
    {
        cout << "You've failed." << endl;
    }

}
for (index = 1; index < 21; index++)

Something here is wrong. Computers don't think like we humans do.
Is it acceptable to use index as a counter both in lines 22 and 40? Other than that I am missing why that statement is a problem.
Yeah the way you used it is totally fine. The value is off though. What is the default "first position" value for a computer?
Oh! Right on! Thank you so much.
Topic archived. No new replies allowed.