Multiple Choice Test

I am currently working on a program to simulate a multiple choice test. I created two arrays one for correct answers, and one for user answers. I also created a function to compare arrays. If 8 or more user answers match the "student" passes. What I am having trouble with is getting the function to compare the arrays. I would appreciate some guidance. Thank you.
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

#include<iostream>
using namespace std;
char check_answers(char student_answers >= const char correct_answers); //function to compare user answers to correct answers.
int main()
{

	
	const char correct_answers[10]=(B, D, A, A, C, A, B, A, C, D); //correct answer array
	char student_answers[10]; //array for users answers
	char i; //char for loop.
	


	for (i = 0; i < 11; i++) //loop to gather user answers.
	{
		cout << "Answers to questions are: A, B, C, D." << endl;
		cout << "Please enter you answer.\n\n"
			<< "A\n"
			<< "B\n"
			<< "C\n"
			<< "D\n\n";
		cin >> student_answers;

		if (i = A)         //if else if statement to collect student data.
			return i;
		else if (i = B)
			return i;
		else if (i = C)
			return i;
		else if (i = D)
			return i;
		else
			cout << "Valid answers are A, B C, or D." << endl;
	}
	
	char check_answers(const char correct_answers == char user_answers); //implentation of function to compare arrays.
	
		for (i = 0; i < 11; i++)
		{
			if (student_answers >= 8 correct_answers)
				cout << "Congratulations you pass!" << endl;
			else
				cout << "Sorry you have not passed the exam." << endl;
		break;

	}
		


		//need a code to end program if student enters invalid answer 3 times.





	system("pause");
	return 0;
}
  
You are not using functions correctly.

http://www.cplusplus.com/doc/tutorial/functions/

There are a lot of others problems also with your loops and uses of arrays.

Some changes are commented.

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

int check_answers(char student_answers[], const char correct_answers[]); // fixed prototype

const int SIZE = 10; // array size

int main()
{


	const char correct_answers[SIZE] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' }; //changed () to {}, added ''
	char student_answers[SIZE]; //array for users answers
	char i, temp;
	int numCorrect;



	for (i = 0; i < SIZE; i++) //changed to 10
	{
		cout << "Answers to questions are: A, B, C, D." << endl;
		cout << "Please enter you answer.\n\n"
			<< "A\n"
			<< "B\n"
			<< "C\n"
			<< "D\n\n";
		cin >> temp;
		temp = toupper(temp);

		while (temp != 'A' && temp != 'B' && temp != 'C' && temp != 'D') // check for valid entry
		{
			cout << "In valid answer" << endl;
			cout << "Answers to questions are: A, B, C, D." << endl;
			cout << "Please enter you answer.\n\n"
				<< "A\n"
				<< "B\n"
				<< "C\n"
				<< "D\n\n";
			cin >> temp;
			temp = toupper(temp);
		}

		student_answers[i] = { temp }; // input student answer in the array

	}

	numCorrect = check_answers(student_answers, correct_answers); //implentation of function to compare arrays.

	if (numCorrect >= 8)// if they got 8 or more right
		cout << "Congratulations you pass!" << endl;
	else
		cout << "Sorry you have not passed the exam." << endl;



	//need a code to end program if student enters invalid answer 3 times.


	return 0;
}

int check_answers(char student_answers[], const char correct_answers[]) // check answer function
{
	int numCorrect = 0, i;

	for (i = 0; i < SIZE; i++)//move through arrays
	{
		if (student_answers[i] == correct_answers[i])//campare answers
		{
			numCorrect++;//if they were the same
		}
	}
	return numCorrect;
}
Last edited on
Joe -

Thank you, for the help and commenting to point out errors. Very helpful. I do have a question, I see the word "Temp" in the code. Is this an integer you made up, or is it in a library?
closed account (48T7M4Gy)
Here is another way of going about it. It can be built on to make it more user friendly - reject invalid attempts, accept upper or lower case, an addition loop to allow several tries, using more functions.

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

using namespace std;

int check_answers( const char [], const char [], const int);

int main()
{
    const int no_questions = 10;
    char correct_answers[] = {'B','D','A','A','C','A','B','A','C','D'};
    char student_answers[no_questions] = {0};
    int no_correct = 0;
    
    // ATTEMPT TEST
    for (int i = 0; i < no_questions; i++)
    {
        cout << "Question " << i + 1 << '\n';
        cout << "Please enter you answer A, B, C or D: ";
        
        cin >> student_answers[i];
    }
    
    // CHECK RESULTS
    no_correct = check_answers( correct_answers, student_answers, no_questions);
    cout << "You scored " << no_correct << '\n';
    
    if( no_correct >=8 )
        cout << "Congratulations you pass!\n";
    else
        cout << "Sorry you have not passed the exam\n";
    
    return 0;
}

int check_answers( const char correct_answer[], const char attempt[], const int count)
{
    int correct = 0;
    
    for( int i =0; i < count; i++ )
    {
       if(attempt[i] == correct_answer[i])
           correct++;
    }
    
    return correct;
}
Topic archived. No new replies allowed.