Help with arrays and functions.

This is the assignment:


The local Driver's License Office has asked you to write a program that grades the written
portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the
correct answers:
1. B 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. A 13. D 18. D
4. A 9. C 14. A 19. D
5. C 10. D 15. D 20. A
Your program should store the correct answers shown above in an array. It should ask the user
to enter the student's answers for each of the 20 questions, and the answers should be stored in
another array. After the student's answers have been entered, the program should display a list
showing the question numbers of the incorrectly answered questions, the total number of
correctly answered questions, and the total number of incorrectly answered questions. It should
then display a message indicating whether the student passed or failed the exam. (A student must
correctly answer 15 of the 20 questions to pass the exam.)
Create a function named inputAnswers to obtain the 20 answers from the user.
Create another function called checkAnswers to compare the user's answers to the correct
answers. The function should display a list showing the question numbers of the inncorrectly
answered questions and return the number of correct answers.
Create 1 more function called displayResult that takes the number of correct answers as a
parameter and determines if the user passed and displays the total number of correctly answered
questions and the total number of incorrectly answered questions.
Input Validation: Only accept the letters A, B, C, or D as answers. You may also allow lowercase
letters.


This is the problem:
I get garbage as output for the student_input_answer.

This is the code:
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Madeline Schimenti
// Summer 2014
// Chapter 6 & 7 Program

#include <iostream>
using namespace std;
#include <cctype>



int main()
{
    
    // Constants and variables
    const int NUM_QUESTIONS = 20;
    const int MIN_CORRECT = 15;
    char answers[NUM_QUESTIONS] = {
        'B', 'D', 'A', 'A', 'C',
        'A', 'B', 'A', 'C', 'D',
        'B', 'C', 'D', 'A', 'D',
        'C', 'C', 'B', 'D', 'A'
    };
    
    char a = 'A';
    a=toupper(a);
    
    char b = 'B';
    b=toupper(b);
    
    char c = 'C';
    c=toupper(c);
    
    char d = 'D';
    d=toupper(d);
    
    

    // Array for input
    char studentAnswers[NUM_QUESTIONS];
    void inputAnswers(char[], int);
    void gradeAnswers(char[], char[], int, int);
    void displayAnswers(char[], char[], int);
    
    cout<< "Welcome to the Driver's License Test! \n";
    cout << "You must correctly answer at least 15\nout of the 20 questions to pass!\n";
    
    
    
    
    // call
    
    
    inputAnswers(answers, NUM_QUESTIONS);
    gradeAnswers(answers, studentAnswers, NUM_QUESTIONS, MIN_CORRECT);
    displayAnswers(studentAnswers, answers, NUM_QUESTIONS);
    
    
    return 0;
}





// INPUT FUNCTION
void inputAnswers(char studentAnswers[], int NUM_QUESTIONS)
{
    for (int index = 0; index < NUM_QUESTIONS; index++)
    {
        
        cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
        cin >> studentAnswers[index];
        
        //Input validation of users answers
        while (studentAnswers[index] != 'A' && studentAnswers[index] != 'a' && studentAnswers[index] != 'B' && studentAnswers[index] != 'b' && studentAnswers[index] != 'C' && studentAnswers[index] != 'c' && studentAnswers[index] != 'D' && studentAnswers[index] != 'd')
        {
            cout << "You must enter A, B, C, or D\n";
            
            cout << "\nPlease enter your answer for question " << (index + 1) << ": ";
            cin >> studentAnswers[index];
        }
        
    }
}


// Function gradeAnswers
void gradeAnswers(char CORRECTanswer[], char student_input_answer[], int NUM_QUESTIONS, int MIN_CORRECT)

{
    
    int correctAnswers = 0;
    cout << "\nYou must have at least 15 correct to pass.";
    
    // Grade each answer
    for (int index = 0; index < NUM_QUESTIONS; index++)
    {
        
        using namespace std;
        
        
        if (student_input_answer[index] == (CORRECTanswer[index]))
            
            correctAnswers++;
        
        
        
    }
    if (correctAnswers >= MIN_CORRECT)
    {
        cout << "\nCongratulations! You passed the exam!\n\n";
    }
    
    else
    {
        cout << "\nSorry, you did not pass the exam." << endl;
    }
    
    
    
    
}



// Display Function

void displayAnswers(char student_input_answers[], char CORRECTanswer[], int NUM_QUESTIONS)
{
    
    cout << "\n\nIncorrect Answers:\n"
    
    << " -----------------";
    
    for ( int index = 0; index < NUM_QUESTIONS; index++)
	{
		
		if (student_input_answers[index] != CORRECTanswer[index])
            cout << "\n" << index + 1 << "." << student_input_answers[index];
        
	}
    
    
    
}


This is the output:




Incorrect Answers:
-----------------
1.\250
2.
3.\300
4._
5.\377
6.
7.
8.
9.
10.
11.
12.
13.\377
14.
15.
16.
17.\210
18.\371
19.\277
Last edited on
Let's start by cleaning up your code.

Remove #include <cctype> . You never use it.

Move lines 14-22 outside of main() to right after using namespace std; and make the array const. You could also change 'NUM_QUESTIONS' and 'MIN_CORRECT' to be #define 's. ( #define NUM_QUESTIONS 20 )

Lines 24-34 do nothing. Remove them.

Lines 40-42 should be outside of main() right after your constants. You should also change their input parameters to look like:
1
2
3
void inputAnswers ( char studentAnswers[] );
void gradeAnswers ( char studentAnswers[] );
void displayAnswers ( char studentAnswers[] );

You should now change lines 53-55 to read:
1
2
3
inputAnswers ( studentAnswers );
gradeAnswers ( studentAnswers );
displayAnswers ( studentAnswers );

Change lines 66, 88, and 128 accordingly. We want all of our references to the same thing to have the same name, so make sure to change their names inside the functions as well. Post your code again once you've gotten that far.
I followed your instructions, except for the #define because we haven't learned that in class yet. After help from here and my instructor and much much debugging and thought.... I fixed it! Thank you!
@Yay295
You could also change 'NUM_QUESTIONS' and 'MIN_CORRECT' to be #define 's.

Yes, you could. However, const int is the preferred C++ idiom. #define is primarily a C idiom.

@OP
What is the point of lines 25,28,31,34? The characters you're upshifting are already upper case.
Last edited on
@AbstractionAnon

The point of the lines 25-34 was me experimenting with toupper. It was something that someone suggested to me so I could convert the input if entered as a lowercase so that functions could compare the lowercase input with the uppercase data in the array. Since we haven't learned that in class (yet?) and I didn't know how to use it properly, I ditched that and replaced it with this:
1
2
 if ('a' <= student_input_answers[index] && student_input_answers[index] <= 'd')
                student_input_answers[index] = char(((int)student_input_answers[index]) - 32);


And that worked beautifully.
And this is where I would suggest using toupper() instead of subtracting 32. Subtracting 32 works, but is somewhat obscure.

You can rewrite those two lines as:
 
    student_input_answers[index] = (char)toupper(student_input_answers[index]);
There's also the issue that subtracting 32 will yield bad results if the char isn't a lowercase character.
I see what you mean. It seems to work fine when I use a capital letter. It also works when I input a combination of capital and lowercase. I also have the input validation so only lower and upper a - d. The if statement is only supposed to subtract 32 if a lowercase is entered.
I will try using this:

student_input_answers[index] = (char)toupper(student_input_answers[index]);

Thank you. I was experimenting with how to use the toupper, that's kind of what I was looking for.

Topic archived. No new replies allowed.