# Comparing 2 char arrays

• 02-25-2010
Rasher
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[4]

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[0] = '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[0] = 'R'

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

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
• 02-25-2010
laserlight
Consider using nested loops, then check if guess[i] == answer[j], where the loop indices are named i and j.
• 02-25-2010
Rasher
i tried this and failed
Code:

```        int a=0;         int i;         int j;         for(i=0;i<4;i++)         {                 if(userguess[0]==answer[i])                 {                         a++;                 }                 for(j=0;j<4;j++)                 {                         if(userguess[j]==answer[j])                         {                                 a--;                         }                 }         }         for(i=0;i<4;i++)         {                 if(userguess[1]==answer[i])                 {                         a++;                 }                 for(j=0;j<4;j++)                 {                         if(userguess[j]==answer[j])                         {                                 a--;                         }                 }         }         for(i=0;i<4;i++)         {                 if(userguess[2]==answer[i])                 {                         a++;                 }                 for(j=0;j<4;j++)                 {                         if(userguess[j]==answer[j])                         {                                 a--;                         }                 }         }         for(i=0;i<4;i++)         {                 if(userguess[3]==answer[i])                 {                         a++;                 }                 for(j=0;j<4;j++)                 {                         if(userguess[j]==answer[j])                         {                                 a--;                         }                 }         }         white = a;```
Epic fail lol
• 02-25-2010
Rasher
just read your post again and can already see thats not what you ment
• 02-25-2010
Rasher
just working through my logic on paper and realised its wrong which puts me back to square one anyone have any ideas?