Question about arrays

Hello, I am doing this program where I have an array of char type that is initialized with 20 characters

Like so:
1
2
const int SIZE = 20;
char answers[SIZE] = {'A', 'B', C', 'D'...//and so forth} 


and I have to enter 20 of my own characters from A to D, so my question is how would I compare the two arrays so that I can tell how many of the user's input was the same as the answers array? Like if I got 15 out of 20 characters the same.
Use strings.

Sorry, that was a little flip. Basically, just make two arrays, one is const char answers[SIZE] = {..} and the other is char response[SIZE];
When you get input from the user, stuff it into one of the char variables in the second array, and then literally just compare them with the == operand.
Last edited on
so I can do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int SIZE = 20;
char answers[SIZE] = {..},
        response[SIZE];


for(int count = 0; count < SIZE; conut++)
{
     cin >> response[count];
}

for(int count = 0; count < SIZE; count++)
{
    if(response[count] == answers[count]
    {
           //Somthing would go here (lol)
     }
}



So how would I display how many are wrong?
So how would I display how many are wrong?

You can create a variable of type int and call it something like:
int matches = 0; (Initalize to zero)
Everytime if(response[count] == answers[count]) evaluates to true, you could increment matches - match++;. Then just output that variable using cout after the loop is done.
Last edited on
Check your syntax. For one thing, {..} will never do anything, so you will have to (and I'm assuming you already knew this, I'm just being redundant) actually put the answers in there the way you did the first time. Second, you have "conut" in there, and you have to make sure that you don't do that kind of thing. That's the exact reason I use 'i' (I think of it as iterator) for when I make a for loop.

Aside from that, you're fairly correct, except I put const char answers[SIZE] = {..} on a separate line for a reason. It's not supposed to be changed, only the responses array is.

Maese909 is right, you just make a variable to increment whenever you do find a correct answer, and if you want incorrect answers, subtract that same variable from SIZE (no need for a new variable).
Okay, thanks for the help. I was able to complete the program :D
Topic archived. No new replies allowed.