Displaying lists from arrays.

Apr 25, 2015 at 12:44pm
I am really having trouble with the last for loop. I am supposed to display the question numbers that were answered incorrectly, not the actual answers. Thanks

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

int main() {
char answers[20], usersAnswers[20];
int correct = 0, incorrect = 0;
answers[0] = 'A';
answers[1] = 'D';
answers[2] = 'B';
answers[3] = 'B';
answers[4] = 'C';
answers[5] = 'B';
answers[6] = 'A';
answers[7] = 'B';
answers[8] = 'C';
answers[9] = 'D';
answers[10] = 'A';
answers[11] = 'C';
answers[12] = 'D';
answers[13] = 'B';
answers[14] = 'D';
answers[15] = 'C';
answers[16] = 'C';
answers[17] = 'A';
answers[18] = 'D';
answers[19] = 'B';


	cout <<"Driver's License Exam"<< endl;


//Loop allowing the user to enter their answers //
for (int i = 0; i < 20; i++)
{
cout << "Enter your answer: ";
	cin >> usersAnswers[i];
if(usersAnswers[i] == 'A')
	cin >> usersAnswers[i];
else if(usersAnswers[i] == 'B')
	cin >> usersAnswers[i];
else if(usersAnswers[i] =='C')
	cin >> usersAnswers[i];
else if(usersAnswers[i] == 'D')
	cin >> usersAnswers[i];
else
	cout << "You must enter A, B, C, D "<< endl;

}





for(int i = 0; i < 20; ++i) {
if(usersAnswers[i] == answers[i])
++correct;
else
++incorrect;
}

if(correct >= 15)
	cout <<"You Passed!!!"<< endl;
else
	cout <<"You Failed!!!"<< endl;



cout << endl;
cout << "You got " << correct << " correct" << endl;
cout << "You got " << incorrect << " incorrect" << endl;

for (int i = 0; i < 20; ++i)
    {
        if (usersAnswers[i] != answers[i])
           
			cout<< '#' << i+1 << " was answered incorrectly" << '\n';    
    }
return 0;
}
Last edited on Apr 25, 2015 at 1:21pm
Apr 25, 2015 at 12:53pm
If you want to output the question number instead of the answer, then... do that.

1
2
3
4
5
    for (std::size_t i=0; i<20; ++i)
    {
        if (usersAnswers[i] != answers[i])
            std::cout << i+1 << '\n';    // assuming you want numbers in the range 1 to 20.
    }
Apr 25, 2015 at 1:00pm
You need to save the incorrect answer in another loop
For that you have tho change your line 42-43 somewhat like this

1
2
3
4
5
6
else
{
incorrectNo[k]=i;
k++;
++incorrect;
}

Don't forget to add the incorrectNo variable :D

Then you can change your line 57-60 somewhat like this

1
2
for(int i=0;i<k;i++)
cout<<incorrectNo[i]<<endl;




Last edited on Apr 25, 2015 at 1:01pm
Apr 25, 2015 at 1:03pm
Thank you so much.
Apr 25, 2015 at 1:19pm
Welcome :D
Apr 25, 2015 at 1:21pm
I am having one last problem with my program. I cannot get the input validation to work correctly. It is making me input a char twice if it is enter correctly. Somewhere in there something if off. I have updated my code up top.
Apr 25, 2015 at 1:52pm
You are checking the answer and again asking the user to answer it... You have to take the input and then check for its validity...

this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (int i = 0; i < 20; i++)
{
cout << "Enter your answer: ";
	cin >> usersAnswers[i];
if(usersAnswers[i] == 'A')
	cin >> usersAnswers[i];
else if(usersAnswers[i] == 'B')
	cin >> usersAnswers[i];
else if(usersAnswers[i] =='C')
	cin >> usersAnswers[i];
else if(usersAnswers[i] == 'D')
	cin >> usersAnswers[i];
else
	cout << "You must enter A, B, C, D "<< endl;

}


should be

1
2
3
4
5
6
7
8
9
10
11
12
13
reattempt:
cout << "Enter your answer: ";
	cin >> usersAnswers[i];
switch (usersAnswers[i])
{
case 'A': case 'B': case 'C': case 'D':
break;
default:
cout<<"cout << "You must enter A, B, C, D only"<< endl;";
goto reattempt;
break;//I dont think this break would be necessary would it be?

}


here I mention two things

1.switch-case was deliberately preferred over if-else as it is shorter... You could also have used ||(OR) operator but that would also be very long...

2.I have used a goto it is a bad habit.... You should sincerely try to avoid it on big codes... Alternatively you could have used a while loop

hope it helps
Last edited on Apr 25, 2015 at 2:43pm
Apr 25, 2015 at 3:09pm
I tried to do a while loop. I don't understand what the heck is going on.


1
2
3
4
5
6
7
8
for (int i = 0; i < 20; i++)
{
cout << "Enter your answer: ";
	cin >> usersAnswers[i];
while(usersAnswers[i] != 'A', 'B', 'C', 'D')
	cout <<"You must enter A, B, C, D "<< endl;
cin >> usersAnswers[i];
}
Apr 25, 2015 at 3:17pm
check your line 5 it should be like this

 
while(usersAnswers[i] != 'A' || usersAnswers[i] != 'B' || usersAnswers[i] != 'C' || usersAnswers[i] != 'D')


you have to use || (OR) operator instead of comma (,) operator
Topic archived. No new replies allowed.