Comparing Arrays - for loop issue?

Hi there,

This is my first post and thanks for taking a look. So let's dive in....

I am comparing two arrays to see if the student[] matches the answers[]. I've got the for loops set up right I think but when i enter in the correct answers I only receive a "You got 4 questions right" when it should say I got 10 right. Anyone see why its not responding correctly?

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

int grade(const char master[], const char student[], int);

void main()
{
	int howMany;

	char answers[] = {'a','b','b','a','c','b','a','a','b','c'};
	char student[10];

	cout<<"How many questions? ";
	cin>>howMany;

	cout<<"Enter student answers: ";
	cin>>student[0];

	grade(answers,student,howMany);

}

int grade(const char master[], const char student[], int howMany)
{
	int correct = 0;
	for(int i = 0; i<howMany; i++)
	{
		for(int j = 0; j<howMany; j++)
		{
			if(master[i] == student[j])
				correct++;
		}
		
	}
	cout<<"You got "<<correct<<" correct"<<endl;
	return 0;
}
You're not filling the student array, I'm suprised you get 4 right when you only initialize student[0].
Try looping 10 times in order to fill the student array
aha! that helped and then i was able to take out the extra for loop in my function and i switched the comparison so it was making sure the students answers were checked against the master key...it always helps to get another perspective on things...Thank you Warnis kudos!
Topic archived. No new replies allowed.