Looping through and comparing two string arrays

Hi,
I'm having trouble with a section of my code where i'm asked to loop through two string arrays, and compare the two for matching answers, and print incorrect next to ones that don't match.
I'm not the best with loops so i don't really know where to get started here.
Any help would be appreciated.
The starting point is the data. What do the two arrays look like? Are they the same size or different sizes etc. What values does each contain. Are they fixed or is one fixed and the other supplied by the user or what?
They are both fixed. Both the same size of 11. They both contain the same words, but one has 2 different words that i want to check for.
Do you have actual code, and specific examples of what you mean by "same words, but one has 2 different words"?

Actual code would help.

Also - what did you try so far, yourself.
So one array looks like this
string key[11] = { "c++","for","if","variable","function","return",
"array","void","reference","main","prototype"};
while the other array is read from a text file which says
C++, for, if, variable, function, cheese, array, robots, reference, main, prototype

I'm not sure where to begin with the loop, so i'm just looking for some kind of example i can maybe use to form my own code.
OK, thank you for that. It is becoming clearer now.

Something like this, though it is only an idea, it needs a bit more work to make it meaningful:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     const int size = 11;
     
     string key[size] = { "c++", "for", "if", "variable", "function", "return", 
        "array", "void", "reference", "main", "prototype" };
     string answer[size];
     
     // assume second array has been filled by reading file or something similar.
     
     for (int i=0; i<size; ++i)
     {
        if (key[i] == answer[i])
        {
            cout << "ok" << endl;
        }
        else
        {
            cout << "incorrect" << endl;
        }
     }


I used the constant named size to avoid having the magic number 11 scattered at multiple places throughout the code. Doing it this way is easier to maintain if it needs to be changed, and keeps things consistent.

If you have difficulty understanding loops, feel free to ask, but also have a read of the tutorial which goes into detail:
http://www.cplusplus.com/doc/tutorial/control/

edit - typing mistake in code corrected.
Last edited on
Thank you for that example, that clears a lot up for me, I don't know why I thought it would be more complicated than that. I'll try to implement it into my code when I get home and if I have anymore questions I'll make sure to ask.
Topic archived. No new replies allowed.