Showing the ranks of certain elements in an array.

This program is a quiz with all the answers in a key array. The user inputs answers in answer array. It then checks to see if at least 15 were correct. I've been trying to figure out a way to output the question numbers that were wrong. I've tried to use an else statement for wrong answers to put the value of the counter in another array. But the output has just been garbage numbers. I'm not getting why it is doing that.

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
  #include <iostream>
using namespace std;

int main()
{
	const int size = 20;
	char answer[size];
	char key[size] = {'A','D','B','B','C','B','A','B','C','D','A','C','D','B','D','C','C','A','D','B' };
	static int score = 0;
	int wrong[size];
	


	for (int i = 0; i < size; i++)
	{
		cout << "Question " << i + 1 << ": ";
		cin >> answer[i];


		while (answer[i] != 'A' && answer[i] != 'B' && answer[i] != 'C' && answer[i] != 'D')
		{
			cout << "Enter valid answer: " << ": ";
			cin >> answer[i];


		}
		if (answer[i] == key[i])
		{
			score += 1;
		}
		else
			wrong[i] = i;
		
	}
	
	if (score < 15) 
	{
		cout <<"You didn't pass." << endl;
		
	}
	else 
	{
		cout << "Congratulations, you passed!" << endl;
	}
	
	cout << "You missed the following questions: \n " << wrong <<".\n";
	
	return 0;
}
you can't print an array with cout.
you need to loop over it and print it that way.
consider, though...
bool wrong[size]{}; //initially all false..
... code...
else wrong[i] = true;
...
for(int i = 0; i < size; i++)
if(wrong[i])
cout << "blah blah wrong blah" << i+1 << endl;
Oh thanks that was it. I didn't consider that other way seems more efficient.
Efficiency aside, your design is difficult to implement without a variable sized container to store the question numbers. Its a good idea, but depending on whether you know vector<> or not, frustrating to implement.
Topic archived. No new replies allowed.