Beginners Quiz

Hello, I'm working on a little quiz program for fun and practice.
Now I've come across a slight hinder.....as it's a quiz with multiple answers I need the program to be able to notice the right inputs against the wrong inputs and I'm not really sure if I should be using a IF statement or maybe a SWITCH statement, anyway this is the code I have so far, any help would be great!

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
//StartQuiz.h
//This is a seperate header file including the actual quiz questions
#include <iostream>
#include <string>
using namespace std;

void StartQuiz ()
{
     system("cls");     //Clears screen
     char i1, i2, i3;
     
     cout << "What are the three basics in data security?\n\n"
          << "a) Protect Information\n"
          << "b) Create access points for wireless communication\n"
          << "c) Ensure operation\n"
          << "d) Prevent robberies and hacking\n"
          << "e) Divide network into different subnets\n\n"
          << "Answer: ";
     cin >> i1 >> i2 >> i3;
     if (i1 == 'a' && i2 == 'c' && i3 == 'd')  
     {
               //This is where I have trouble as program will only 
               //recognize i1 as 'a' i2 as 'c' and i3 as 'd'
               cout << " Correct!\n";
     }
     else
     
     cout << " Wrong Answer!! It's a, c, and d\n\n";
}
You could read the three answers into an array and then sort that array, so that the correct inputs can only be represented in one form. You can use std::sort from <algorithm> for sorting.
That code works fine for me. So I would keep it like that. The only thing you're going to have to think about is how you are going to change the constants on line 20, 'a', 'c' and 'd'.

And are all the questions going to have 3 answers?

If they are going to all have 3 answers, maybe make a function where you pass the question number and some answer variables by reference.

Here's something I wrote to show 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
#include <iostream>		// cout
#include <conio.h>		// _kbhit()
using namespace std;

void getAnswers( int questionNumber, char &ans1, char &ans2, char &ans3 )
{
	switch( questionNumber )
	{
		case 1:
			/* if it's question 1 you want the answers for... */
			ans1 = 'a';
			ans2 = 'c';
			ans3 = 'e';
			break;

		case 2:
			break;

		case 3:
			break;

		case 4:
			break;
	}
}


int main()
{
	char userAns1, userAns2, userAns3,
		ans1, ans2, ans3;

	/* output question */

	/* get the answers */
	//the values of ans1, ans2 and ans3 will change to the values
	//within the getAnswers function, as they were passed bt reference
	getAnswers( 1, ans1, ans2, ans3 );

	cin >> userAns1 >> userAns2 >> userAns3;

	//if you only have one statement under an if/loop, you don't need the curly braces {}
	if( ( userAns1 == ans1 ) && ( userAns2 == ans2 ) && ( userAns3 == ans3 ) )
		cout << "Correct answer!";
	else
		cout << "Incorrect!";

	//press any key to exit...
	std::cout << "\n\nPress any key to exit...";

	while( ! _kbhit() )
	{ /* do nothing - wait for key press */ }
	
	return 0;
}


Hope this helps!
@Lynx876 This seems like just what I was looking for, thanks.

All the questions will not have 3 answers but will be varied, would this still work using the switch statement?
With a varying total of answers. Maybe something like 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>		// cout
#include <conio.h>		// _kbhit()
using namespace std;

int getAnswers( int questionNumber, char &ans1, char &ans2, char &ans3 )
{
	int answers = 0;

	switch( questionNumber )
	{
		case 1:
			/* if it's question 1 you want the answers for... */
			ans1 = 'a';
			ans2 = 'c';
			ans3 = 'e';

			//there are 3 answers
			answers = 3;

			break;

		case 2:
			ans1 = 'b';
			ans2 = 'd';

			//there are 3 answers
			answers = 2;

			break;

		case 3:
			break;

		case 4:
			break;
	}

	return answers;
}


int main()
{
	char userAns1, userAns2, userAns3,
		ans1, ans2, ans3;

	/* output question */

	/* get the answers */
	//set totalAnswers from the function return
	int totalAnswers = getAnswers( 2, ans1, ans2, ans3 );

	switch( totalAnswers )
	{
		//question has one answer
		case 1:
			cin >> userAns1;

			if( userAns1 == ans1 )
				cout << "Correct answer!";
			else
				cout << "Incorrect!";
			break;

		//question has two answers
		case 2:
			cin >> userAns1 >> userAns2;

			if( ( userAns1 == ans1 ) && ( userAns2 == ans2 ) )
				cout << "Correct answer!";
			else
				cout << "Incorrect!";
			break;

		//question has three answers
		case 3:
			cin >> userAns1 >> userAns2 >> userAns3;

			if( ( userAns1 == ans1 ) && ( userAns2 == ans2 ) && ( userAns3 == ans3 ) )
				cout << "Correct answer!";
			else
				cout << "Incorrect!";
			break;
	}

	//press any key to exit...
	std::cout << "\n\nPress any key to exit...";

	while( ! _kbhit() )
	{ /* do nothing - wait for key press */ }
	
	return 0;
}


Very possibly, not the best way to do it. This is just off the top of my head from what I already gave you.

EDIT:
Line 47, might as well take that out of there and output the question in the switch, within the getAnswers() function :/ ahaha.
Last edited on
Thanks again, very helpfull :)
Topic archived. No new replies allowed.