creating a test with multiple choices?

first off, im a complete noob, i only have about 10 hours of actual programming so bear with me.

im trying to write a test that has multiple choice answers.
i know this can be done with the switch statement, but i want to do it without using that.

ive spent dang near 5 hours trying to get it, but i just cant, is it even possible? here is my code.

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

using namespace std;

int main()
{
	cout << "\t\t SIMPLE TEST\n";
	cout << "what is 2 + 2? \n";
	cout << " A) 2";
	cout << "\n B) 6";	// making the question
	cout << "\n C) 4";
	cout << "\n D) 0";

	cout << "\n";

	char a,
		 b,
		 c,			// the possible answers
		 d;

	if (cin >> c)
	{
		cout << "that is correct!\n";  // this is where im confused.. 
	}
	else
	{
		cout << "wrong\n";
	}
	system("PAUSE");
	return 0;
}
p.s. , all the program does is output "that is correct!" even when i input an integer...
i know this can be done with the switch statement, but i want to do it without using that

a switch is better when you have several options but in this case you could use the conditional/ternary operator: https://www.tutorialspoint.com/cplusplus/cpp_conditional_operator.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

//using namespace std;
//http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

int main()
{
	std:: cout << "\t\t SIMPLE TEST\n";
	std:: cout << "what is 2 + 2? \n";
	std::cout << " A) 2" << "\n B) 6" << "\n C) 4"<< "\n D) 0 \n";

	std::cout << "choose your answer: \n";
	char answer{};
	std::cin >> answer;//see below link for some input validation ideas and modify for char:
	//
	//http://www.cplusplus.com/forum/beginner/206234/
    //http://www.cplusplus.com/forum/beginner/211726/

	std::cout << ( (answer == 'A') || (answer == 'a') ? "Correct \n" : "Incorrect \n" );
}
closed account (48T7M4Gy)
In a very basic form this is what you need to do.

There are lots of things you can add, like error checks, upper and lower case, having another try, but get this part under control first.

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

using namespace std;

int main()
{
    cout << "     SIMPLE TEST\n";
    cout << " What is 2 + 2? \n";
    cout << " A) 2 \n";
    cout << " B) 6 \n";	// making the question
    cout << " C) 4 \n";
    cout << " D) 0 \n\n";
    
    char answer;
    cin >> answer;
    
    if (answer == 'C')
    {
        cout << "That is correct!\n";  // this is where im confused..
    }
    else
    {
        cout << "wrong\n";
    }
    
    system("PAUSE");
    return 0;
}


PS '\n' goes better at the end of the line for clarity and \t are a pain to be avoided especially when stating off. Tabs are a painful nuisance that just clutter up good code.

My late apology to gunner because I just saw the valuable addition showing how to handle the upper and lower case aspects.
Last edited on
A key idea here is that the below line is probably not what you want. You probably are trying to evaluate whether the answer is 'c', but that's doing something a bit different. To keep a long story short, that condition is always evaluating as true, which is why the "That is correct!" message always shows.

if (cin >> c)

Instead, you should define a variable to hold the answer, then get the answer using cin. cin will automatically pause and allow the user to enter an answer. Taken from Kemort's example above:

1
2
char answer;
cin >> answer;


The answer that user enters will be stored in the variable "answer". Then, you want to see if the answer is correct, ie if the answer equals 'c'.

if (answer == 'c')

Notice how Kemort puts single quotes around each possible answers ('c' as opposed to c). That's because these single letters are a character data type, and you want to mark them as such. If you just write c, with no quotes, the program won't recognize it as a character.

Hope this is somewhat helpful.
Last edited on
Topic archived. No new replies allowed.