# Thread: Comparing 2 char arrays

1. ## Comparing 2 char arrays

im in the final stages of writing mastermind and im down to the white pegs
i have 2 char arrays with 4 elements in each i.e

userguess
answer the answer is randomly generated

i need to compare them with each other so if the user guesses for example R i need to search through answer and see if there are any R's more importantly which location they are in and if they are in the same location i.e

userguess = 'R'
answer = 'R'

if they are in the same location i need to ignore it as that would generate a black peg however if they are in different locations i.e

userguess = 'R'
answer = 'R' or answer,answer

then i need to add 1 to my white count. I got that working however my problem comes when there is more than 1 'R' in answer[x] i tried using the following method which went wrong

if guess = answer,answer,answer
guess = answer,answer, answer
etc.......

i know thats not the correct syntax! somehow i think i need to change it so if answer1, 2 or 3 are the same it deducts 1 from the white count? i tried this with lots of if statements it compiled but crashed every time it tried to work out the white count. i would post my code but its seriously long. Can anyone point me in the right direction even if its a different way of doing it sorry for the essay!! thanks 2. Consider using nested loops, then check if guess[i] == answer[j], where the loop indices are named i and j. 3. i tried this and failed
Code:
```        int a=0;
int i;
int j;
for(i=0;i<4;i++)
{
if(userguess==answer[i])
{
a++;
}
for(j=0;j<4;j++)
{
if(userguess[j]==answer[j])
{
a--;
}
}
}
for(i=0;i<4;i++)
{
if(userguess==answer[i])
{
a++;
}
for(j=0;j<4;j++)
{
if(userguess[j]==answer[j])
{
a--;
}
}
}
for(i=0;i<4;i++)
{
if(userguess==answer[i])
{
a++;
}
for(j=0;j<4;j++)
{
if(userguess[j]==answer[j])
{
a--;
}
}
}
for(i=0;i<4;i++)
{
if(userguess==answer[i])
{
a++;
}
for(j=0;j<4;j++)
{
if(userguess[j]==answer[j])
{
a--;
}
}
}
white = a;```
Epic fail lol 4. just read your post again and can already see thats not what you ment 5. just working through my logic on paper and realised its wrong which puts me back to square one anyone have any ideas? Popular pages Recent additions 