Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hello all,

First off, I'll just let you guys know that I'm really new at this.

I need some help with a simple C++ program using arrays and loops to create a 20 question quiz. The only thing the program needs to accomplish is allow the user to enter 20 multiple choice answers A,B,C, or D and compare the user entered answers to a correct set of answers to calculate a percentage of correct answers entered. If anyone could just point me in the right direction, or give me a quick example of what I should have, that would be greatly appreciated.

This is what I have so far.. but its not much.

#include <iostream>
using namespace std;

int main()
{
	const int NUM_QUESTIONS = 20;			// Number of Questions
	char user_answers[NUM_QUESTIONS];		// Array with 20 elements
	char correct_answers[NUM_QUESTIONS];
	int count;								// Loop counter variable 
	int correct;
	char correct_answers[count] = {B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A} 

	cout << "\t\t*******************************\t\t\n";
	cout << "\t\t*****Driver's License Exam*****\t\t\n";
	cout << "\t\t*******************************\t\t\n\n";


	for (count = 0; count < NUM_QUESTIONS; count++)
	{
		cout << (count + 1) << ". ";
		cin >> user_answers[count];
	}
	
	if (user_answers[count]==correct_answers[count])
	{
	correct++;
	}
	
	cout << endl; 
	cout << correct;

	
	return 0;
}


Thanks
Posted
Updated 31-May-10 5:27am
v3

<br />int count; // Loop counter variable<br />int correct;<br />char correct_answers[count] = {B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A}<br />


This can only cause problems. correct_answers[count] should probably be correct_answers[NUM_QUESTIONS] since count is not initialized.
 
Share this answer
 
Something like this
const int NUM_QUESTIONS = 20;		// Number of Questions
char user_answers[NUM_QUESTIONS];	// Array with 20 elements
char correct_answers[] = {"BDAACABACDBCDADCCBDA"}; 
int count;				// Loop counter variable 

int correct;

cout << "\t\t*******************************\t\t\n";
cout << "\t\t*****Driver's License Exam*****\t\t\n";
cout << "\t\t*******************************\t\t\n\n";

correct = 0;
for (count = 0; count < NUM_QUESTIONS; count++)
{
	char answer;

	do
	{
		cout << (count + 1) << ". ";
		cin >> answer;
		answer = toupper(answer);
		if (answer < 'A' || answer > 'D')
			answer = '?';
		cout << endl; 
	} while (answer == '?');
	user_answers[count] = answer;

	if (user_answers[count]==correct_answers[count])
	{
		correct++;
	}
}

cout << endl; 
cout << correct << " answers correct" << endl;

You probably need to add some fancy bits around the repeat of the question but this should cover the basics.
 
Share this answer
 
Comments
issa_mus 8-Jul-10 20:39pm    
Reason for my vote of 1
i think the above code
is very easy

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900