Drivers ed program

I am writing a simple program that takes in the multiple choice responses of A,B,C and D as input and there are a total of twenty questions. I then compare the users responses to the answers. The problem is that the program says they are all wrong even if they are correct. I cant tell we the problem is. Any help would be awesome.

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
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
char exam_answers[20] = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
char users_answers[20];
int num_wrong = 0, num_right = 0;
cout<<"Driver's License Exam Grader\n";
for(int i = 1; i<=20; i++)
{
    char validate;
    cout<<"Enter your answer for question "<<i<<endl;
    cin>>validate;
    validate = toupper(validate);
    if(validate != 'A' && validate != 'B' && validate != 'C' && validate != 'D')
    {
      cout<<"Invalide input. Eneter only:A, B, C, or D. Try again.\n";
      --i;
      continue;
    }
    exam_answers[i] = validate;
}
for(int j=0; j<=20;j++)
{
    if(exam_answers[j] != users_answers[j])
    {
        cout<<"Answer "<<j<<" is incorrect.\n";
        num_wrong++;

    }

}
num_right = 20 - num_wrong;
cout<<"Results (miniumum 15 correct to pass) :\n";
cout<<"You got "<<num_right<<" correct.\n";
cout<<"You missed "<<num_wrong<<".\n";
if(num_right<15)
{
    cout<<"You've failed.\n";
}
else
{
    cout<<"You've Passed.\n";
}


}
line 24 should be:

users_answers[i] = validate;

now I just need to get the two arrays lined up!
Last edited on
char users_answers[20]

for(int i = 1; i<=20; i++)

Arrays start at zero and finish at size -1, so get used to this idiom :

for(int i = 0; i < 20; i++)

This will avoid a segmentation fault from over stepping the array.

Edit:

Also, avoid magic numbers in your code, initialise a const variable & use that:

const int Size = 20;

for(int i = 0; i < Size ; i++)
Last edited on
Topic archived. No new replies allowed.