A quiz

Hey guys, I am working on a multiple choice quiz. I want to know how I can get the program to say if the person is correct or incorrect after answering the question?

I would like to know if there is a way to just say what they missed at the end of the quiz rather than after each question just like a list.

Here is my set up for one question (there will be 10 total):

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>


using namespace std;

int main ()
{
system ("Color 3c");

int ans;

cout<< "Hello there, today you are going to take a QUIZ! \n" <<endl;
cout<< "The quiz will be about the video game Dishonored. \n" <<endl;

cout<< "Question 1: How many awards did Dishonored win? \n" <<endl;

cout<< "1.) - 6 awards, 2.) - 1 award, 3.) - 10 awards, 4.) - 0 awards";
cout<<endl;

cin>>ans;

switch(ans)
{
case '1':
cout<< "Correct!";
break;

case '2':
cout<< "Incorrect!";
break;

case '3':
cout<< "Incorrect!";
break;

case '4':
cout<< "Incorrect!";
break;
}
system ("pause");
system ("cls");


The program runs, but it won't tell the user if they are correct or incorrect.
The reason your code will not output weather the anser is correct or not, is because you are trying to compare an integer to a character. ans is an integer, in your swich you use characters for the case statements.

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
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>


using namespace std;

int main ()
{
	system ("Color 3c");

	int ans;

	cout<< "Hello there, today you are going to take a QUIZ! \n" <<endl;
	cout<< "The quiz will be about the video game Dishonored. \n" <<endl;

	cout<< "Question 1: How many awards did Dishonored win? \n" <<endl;

	cout<< "1.) - 6 awards, 2.) - 1 award, 3.) - 10 awards, 4.) - 0 awards";
	cout<<endl;

	cin>>ans;

	switch(ans)
	{
	case 1:
		cout<< "Correct!" << endl;
		break;

	case 2:
		cout<< "Incorrect!" << endl;
		break;

	case 3:
		cout<< "Incorrect!" << endl;
		break;

	case 4:
		cout<< "Incorrect!" << endl;
		break;
	}
	system ("pause");
	system ("cls");
}


if you want the program to display which questions were answered correctly, and which questions were answered incorrectly at the end of the quiz. You could make an array filled with the correct answers, and store the user answers in another array and compare the two at the end of the program.
Last edited on
closed account (o3hC5Di1)
Hi,

ans is an int, but you are comparing it to chars. Incidentally, You can shorten your switch statement considerable as follows:

1
2
3
4
5
6
7
8
9
10
switch(ans)
{
case 1: //notice the 1, not '1'
cout<< "Correct!";
break;

default: //any other case than 1
cout<< "Incorrect!";
break;
}


Hope that helps.

All the best,
NwN

Topic archived. No new replies allowed.