Problems with arrays for a program to grade a multiple choice test

I have a few questions about my code below. I am trying to write a program that grades the answers that are input, and output the amount they got correct, and whether or not they passes. A 15/20 or better would be passing, and also let them know the question numbers that they got incorrect. I am getting a lot of errors, and a few I am confused about. Why doesn't my char stu_answers[NUM_QUESTIONS] initializer work? It's saying it's undeclared, but I don't want to put a value for it, when I am supposed to be getting the values from the user.

Lastly, after I figure out the errors in this code and make it compile through, how do I make it so it's not case sensitive, and they could input either 'A' or 'a'? Would I have to make another array with the answers in lower case or is there an easier way that I am completely missing? The only thing I've learned so far to make it so it's not case sensitive was using the switch/break.

I appreciate all answers in advance.

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 <iomanip>
#include <cctype>
using namespace std;

void checkAnswers(char[], char[], int, int);

int main()
{
	const int NUM_QUESTIONS = 20;
	const int MIN_CORRECT = 15;
	char answers[NUM_QUESTIONS] = { 
		'A', 'D', 'B', 'B', 'C',
		'B', 'A', 'B', 'C', 'D',
		'A', 'C', 'D', 'B', 'D',
		'C', 'C', 'A', 'D', 'B',
	}
	char stu_answers[NUM_QUESTIONS];

	cout << "Welcome to the Driver's License Office" << endl;
	cout << "Only valid answers will be A, B, C, or D" << endl;
	for (int i = 0; i < NUM_QUESTIONS; i++){	//loop for answers
		cout << "Please enter your answers: " << (i + 1) << ":";
		cin >> stu_answers[i];
	}
	while (stu_answers[replies] != 'A' && stu_answers[replies] != 'B' && stu_answers[replies] != 'C' && stu_answers[replies] != 'D') {
		cout << "You must enter A, B, C, or D\n";

		cout << "Please enter your answers: "
			<< (replies + 1) << ": ";
		cin >> stu_answers[replies];
	}

}



void checkAnswers(char answers1[], char stu_answers1[], int NUM_QUESTIONS, int MIN_CORRECT) {
	//cout << "max: " << NUM_QUESTIONS;
	int correctAnswers = 0;

	//Check the student's replies against the correct answers
	for (int i = 0; i < NUM_QUESTIONS; i++)	{
		if (answers1[i] == stu_answers1[i])
			correctAnswers++;
	}
	//Did they pass or fail?
	cout << "\nYou must have at least 15 correct to pass.";
	if (correctAnswers >= MIN_CORRECT) {
		cout << "\nStudent passed the exam\n\n";
	}
	else {
		cout << "\nStudent failed the exam\n\n";
	}

	//Display a list of the questions that were incorrectly answered.
	cout << "The list below shows the question numbers of the incorrectly";
	cout << " answered questions.\n";
	for (int i = 0; i < NUM_QUESTIONS; i++)	{
		if (answers1[i] != stu_answers1[i])
			cout << "Question # " << i << " is incorrect." << endl;
	}

	//Display the number of correct and incorrect answers provided by the student.
	cout << "\nCorrect Answers = " << correctAnswers << endl;
	cout << "Incorrect Answers = " << NUM_QUESTIONS - correctAnswers << endl;
}
}
Last edited on
Why doesn't my char stu_answers[NUM_QUESTIONS] initializer work? It's saying it's undeclared, but I don't want to put a value for it, when I am supposed to be getting the values from the user.


I think you mean undefined, not undeclared. You should initialize everything, even if the very next thing you do is get user input.

char stu_answers[NUM_QUESTIONS] = {};

Also, what is replies? It isn't declared or defined anywhere in your code.

Concerning upper- and lower-case, you could simply convert the user's input to uppercase.
For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cctype>

int main() {

	char characters[3] = {'A', 'B', 'C'};

	char user_input = 'a';

	user_input = ::toupper(user_input); //Convert to upper-case. If the argument is already upper-case, it does nothing.

	if (user_input == characters[0]) {
		//...
	}
Last edited on
I've made the changes for the user input to be converted to upper-case and it does compile through, but I seem to be getting an error everytime I run it through with my while loop where I ask them to please enter their answer. Everytime, even when I do put in only a, b, c, or d it still says "You must enter A, B, C, D and then has me answer one more times and ends the program, without giving me the result of their test. What is the reason for this?

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

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

void checkAnswers(char[], char[], int, int);

int main()
{
	char characters[4] = { 'A', 'B', 'C', 'D' };
	char user_input = 'a';
	user_input = ::toupper(user_input); //Convert to upper-case. If the argument is already upper-case, it does nothing.

		


	const int NUM_QUESTIONS = 20;
	const int MIN_CORRECT = 15;
	char stu_answers[NUM_QUESTIONS] = {};
	char answers[NUM_QUESTIONS] = {
		'A', 'D', 'B', 'B', 'C',
		'B', 'A', 'B', 'C', 'D',
		'A', 'C', 'D', 'B', 'D',
		'C', 'C', 'A', 'D', 'B',
	};
	

	cout << "Welcome to the Driver's License Office" << endl;
	cout << "Only valid answers will be A, B, C, or D" << endl;
	int i = 0;
	for (i = 0; i < NUM_QUESTIONS; i++){	//loop for answers
		cout << "Please enter your answers: " << (i + 1) << ":";
		cin >> stu_answers[i];
	}
	while (stu_answers[i] != 'A' && stu_answers[i] != 'B' && stu_answers[i] != 'C' && stu_answers[i] != 'D') {
		cout << "You must enter A, B, C, or D\n";

		cout << "Please enter your answers: "
			<< (i + 1) << ": ";
		cin >> stu_answers[i];
	}

}

void checkAnswers(char answers1[], char stu_answers1[], int NUM_QUESTIONS, int MIN_CORRECT) {
	//cout << "max: " << NUM_QUESTIONS;
	int correctAnswers = 0;

	//Check the student's replies against the correct answers
	for (int i = 0; i < NUM_QUESTIONS; i++)	{
		if (answers1[i] == stu_answers1[i])
			correctAnswers++;
	}
	//Did they pass or fail?
	cout << "\nYou must have at least 15 correct to pass.";
	if (correctAnswers >= MIN_CORRECT) {
		cout << "\nStudent passed the exam\n\n";
	}
	else {
		cout << "\nStudent failed the exam\n\n";
	}

	//Display a list of the questions that were incorrectly answered.
	cout << "The list below shows the question numbers of the incorrectly";
	cout << " answered questions.\n";
	for (int i = 0; i < NUM_QUESTIONS; i++)	{
		if (answers1[i] != stu_answers1[i])
			cout << "Question # " << i << " is incorrect." << endl;
	}
	//Display the number of correct and incorrect answers provided by the student.
	cout << "\nCorrect Answers = " << correctAnswers << endl;
	cout << "Incorrect Answers = " << NUM_QUESTIONS - correctAnswers << endl;
}
Last edited on
I've made the changes for the user input to be converted to upper-case


No, you haven't. All I see is that you've copy and pasted a part of the snippet I provided to the top of your main function.

What is the reason for this?

The reason for this is that it's exactly what you're telling your program to do:

1
2
3
4
5
6
7
8
9
10
11
12
int i = 0;
	for (i = 0; i < NUM_QUESTIONS; i++){	//loop for answers
		cout << "Please enter your answers: " << (i + 1) << ":";
		cin >> stu_answers[i];
	}
	while (stu_answers[i] != 'A' && stu_answers[i] != 'B' && stu_answers[i] != 'C' && stu_answers[i] != 'D') {
		cout << "You must enter A, B, C, or D\n";

		cout << "Please enter your answers: "
			<< (i + 1) << ": ";
		cin >> stu_answers[i];
	}


On line 1 you instantiate i, and initialize it to zero. This variable is totally different from the i which you instantiate on the next line in the for loop.

On line 2 you instantiate i, and initialize it to zero. This variable is totally different from the i which you instantiated on the previous line. These two variables are two separate things with different scopes. The i that is instantiated on this line is tied to the scope of this for loop, and will cease to exist once the for loop terminates.

On line 6 you start a while loop. You're accessing variable i (not the for loop one) which was instantiated on line 1. This i is still zero, and since you're treating it as an index and never changing its value, you'll be accessing the 0th index for any of the index operations you're doing in that entire loop.
Last edited on
Topic archived. No new replies allowed.