Reading chars into an array

I am hoping you can give me a little insight into my programming errors. This program should compare user entered answers to the correct answers. It should then output the total number of correct and incorrect answers, and also check for valid answers (A,B,C, or D).

Everything compiles fine, but the program doesn't function the way it should. Attached is my code. I specifically seem to be having an issue with taking characters into the array I defined. How do you set that up?

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
  #include<iostream>
using namespace std;
int main()
{
	const int SIZE = 10;
	char studentAnswers[SIZE];//student answer
	char answer;
	int matchingAnswers = 0; //variable to count correct answers
	int nonvalid = 0;        //variable to count invalid answers  
	char correctAnswers[] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
	for (char i = 0; i < SIZE; i++)
	{
		cout << "Please enter your answers to your Driver's License Exam in capital letters: " << endl;
		cin >> answer[i];
		studentAnswers[i] = answer;

		if (i != 'A' || 'B' || 'C' || 'D')
		{
			nonvalid=nonvalid++;
			cout << "A,B,C, and D are the only valid inputs." << endl;
		}
		if (nonvalid = 3)
		{
			cout << "GOOD BYE" << endl;
			break;
		}
	}
	for (char i = 0; i < SIZE; i++)
	{
		if (studentAnswers[i] == correctAnswers[i])
			matchingAnswers++;
	}
	if (matchingAnswers >= 8)
	{
		cout << "You have passed the exam." << endl;
		cout << "Total number of correct answers: " << matchingAnswers << endl;
		cout << "Total number of incorrect answers: " << 10 - matchingAnswers << endl;
	}
	else
		cout << "You have failed the exam." << endl;
		cout << "Total number of correct answers: " << matchingAnswers << endl;
		cout << "Total number of incorrect answers: " << 10 - matchingAnswers << endl;
return 0;
}
In your for loops you may want int i. In your cin>>answer[i] you can just leave it as cin>>answer. Also, in your if statement if (i != 'A' || 'B' || 'C' || 'D') you actually want to compare the answer the user input, either studenAnswer[i] or answer --> if (studentAnswer[i] !='A' || studentAnswer[i] !='B' || studentAnswer[i] !='C' || studentAnswer[i] !='D' ). In your next if statement change it to if (nonvalid == 3).
Last edited on
Hi!
I think you need to learn more about how to implement your codes 'more'(yeah, its redundant).
Anyway:

1) You might want to use unsigned int in
char i = 0


2)
nonvalid=nonvalid++
;
why not only nonvalid++ :)

3)
nonvalid = 3

it would be == not =

4)Why not just merge the other for loop? They are looping the same indexes so, it would be cleaner and more efficient to use only one loop. :)

5)The last two couts
cout << "Total number of correct answers: " << matchingAnswers << endl;
cout << "Total number of incorrect answers: " << 10 - matchingAnswers << endl;

can be merged into one and added after the if-else statements.

well, hope it helps. :)
Last edited on
Thank you both so much for the replies! I just checked this at work, so I'll have to wait until I get home to make the changes. I definitely need to get better at this, but practice makes perfect!
OK, I applied the changes that you suggested, but my output still implies that the program is having trouble filling the array studentAnswers. If I put the whole string of correct answers in all at once, it says "A,B,C, and D are the only valid inputs. GOOD BYE. You have failed the test..."

If I try to put one answer in and hit enter, the skips to the end and gives me the same output as above. What am I missing?
I've added a cout<<studentAnswers at the bottom, but it only lets me fill the first 3 blocks or the array.

Here is the 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
using namespace std;
int main()
{
	const int SIZE = 10;
	char studentAnswers[SIZE];//student answer
	char answer;
	int matchingAnswers = 0; //variable to count correct answers
	int nonvalid = 0;        //variable to count invalid answers  
	char correctAnswers[] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
	for (char i = 0; i < SIZE; i++)
	{
		cout << "Please enter your answers to your Driver's License Exam in capital letters: " << endl;
		cin >> answer;
		studentAnswers[i] = answer;

		if (studentAnswers[i] != 'A' || studentAnswers[i] !='B' || studentAnswers[i] != 'C' || studentAnswers[i] !='D')
		{
			cout << "A,B,C, and D are the only valid inputs." << endl;
			nonvalid++;
		}
		if (nonvalid == 3)
		{
			cout << "GOOD BYE" << endl;
			break;
		}
	}
	for (char i = 0; i < SIZE; i++)
	{
		if (studentAnswers[i] == correctAnswers[i])
			matchingAnswers++;
	}
	if (matchingAnswers >= 8)
	{
		cout << "You have passed the exam." << endl;
		cout << "Total number of correct answers: " << matchingAnswers << endl;
		cout << "Total number of incorrect answers: " << 10 - matchingAnswers << endl;
	}
	else
		cout << "You have failed the exam." << endl;
		cout << "Total number of correct answers: " << matchingAnswers << endl;
		cout << "Total number of incorrect answers: " << 10 - matchingAnswers << endl;
		cout << studentAnswers;
return 0;
}
Last edited on
I applied the changes that you suggested

You didnt even follow 1, 4, and 5. :/

If I try to put one answer in and hit enter, the skips to the end and gives me the same output as above. What am I missing?

Just change your || to &&. When any of them is true, then it wont go into that statement.

If I put the whole string of correct answers in all at once

You're using for loop, to loop numbers in your char type. That means that it would only get one variable value at a time.
Last edited on
SOLVED! Here is the correct 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace std;
int main()
{
	const int SIZE = 10;
	char studentAnswers[SIZE];//student answer
	int matchingAnswers = 0; //variable to count correct answers
	int nonvalid = 0;        //variable to count invalid answers  
	char correctAnswers[] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
	cout << "Please enter your answers to your Driver's License Exam questions to " << endl;
	cout << "the corresponding numbers.  Please use only capital letters. " << endl;
	for (int i = 0; i < SIZE; i++)
	{
		cout << i + 1<<" - ";
		cin >> studentAnswers[i];

		if (studentAnswers[i] != 'A' && studentAnswers[i] != 'B' && studentAnswers[i] != 'C' && studentAnswers[i] != 'D')
		{
			cout << "A,B,C, and D are the only valid inputs. Please try to input the answer again." << endl;
			nonvalid++;
			--i;

		}
		if (nonvalid == 3)
		{
			cout << "GOOD BYE" << endl;
			break;
		}
	}
	for (int i = 0; i < SIZE; i++)
	{
		if (studentAnswers[i] == correctAnswers[i])

			matchingAnswers++;
	}
	
	if (matchingAnswers >= 8)
	{
		cout << "You have passed the exam." << endl;
	}
	else 
		cout << "You have failed the exam." << endl;
	
		cout << "Total number of correct answers: " << matchingAnswers << endl;
		cout << "Total number of incorrect answers: " << 10 - matchingAnswers << endl;

		while (matchingAnswers >= 8);

	
		
return 0;
}


I'm sorry rjvc, I pasted the wrong code in the above post. I didn't notice it until now.

The || to && switch seemed to fix everything. I added the --i under nonvalid to keep the question count the same if the user entered in an invalid character into an answer. I also combined the correct answer output to clean it up a little.
Topic archived. No new replies allowed.