# Thread: Comparing Pointer and Integers?

1. ## Comparing Pointer and Integers?

Hi I am trying to make a game where you have a secret code that is coded with colors like ROYG (red,orange,yellow,green) and I am having trouble when it tells you when you have a right color in the right spot or a right color in the wrong spot when you guess a color. How can I change my code under the function int comparearray where it will compare pointers to pointers and not integers and give me the correct number of "almost" and "correct".

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROWS    100
#define COLS    4

void generate_code(char codearr[]);
char guess_array[ROWS][COLS];
int comparearray(char codearr[],char guess_array[ROWS][COLS], int check_location[]);
int check_location[];

int main()
{
char codearr[4];
int j;
int win = 0;

srand(time(NULL));

printf("This program has chosen a sequence of 4 colors for you to guess.\n");
generate_code(codearr);
printf("%s", codearr);

for (j= 1; j < 100; j++)
{
scanf("%s", &guess_array[j]);
comparearray(codearr, guess_array, check_location);
printf("%d.     %s\n", j, guess_array[j]);
}
}

void generate_code(char codearr[])
{
int newrand;
int i=0;
srand(time(NULL));
for(i=0 ; i <4 ; i++)
{
newrand = rand()%6;
if (newrand == 0)
codearr[i]='R';
if (newrand == 1)
codearr[i]='O';
if (newrand == 2)
codearr[i]='Y';
if (newrand == 3)
codearr[i]='G';
if (newrand == 4)
codearr[i]='B';
if (newrand == 5)
codearr[i]='W';
}
}

int comparearray(char codearr[],char guess_array[ROWS][COLS], int check_location[])
{
int i=0, outer=0, t=0;
int right = 0, almost = 0;
for(i=0 ; i <4 ; i++)
{
if(codearr[i] == guess_array[i])
{
check_location[i] = 1;
right++;
}
}
for(outer=0 ; outer < 4 ; outer++)
for(i=0 ; i <4 ; i++) //almost
{
if(codearr[outer] == guess_array[i]&&check_location[outer]==0)
{
for(t = 0 ; t < 4 ; t++)
{
if (check_location[t] == 0)
{
for(i = 0 ; i < 4 ; i++)
{
if(codearr[t] == guess_array[i])
{
almost++;
break;
}
}
}
}
check_location[i] = 1;
almost++;
}
}
printf("Right %d , Almost %d\n", right, almost);
return(right);```

2. your description isn't clear. where exactly is the problem? be more specific

3. Random guess

?Wrong code?
Code:
`if(codearr[i] == guess_array[i])`
Maybe right
Code:
`if(codearr[i] == guess_array[i][j])`
I picked j as the second index at random; you might or should use another variable.

Tim S.

4. Originally Posted by dmh2000
your description isn't clear. where exactly is the problem? be more specific
I'm sorry. What I am doing with this program is randomly generating a sequence of 4 colors and having a user guess and depending on what they guess, the program should tell them if they have the correct color in the right spot, or if they have the right color, but the wrong spot.

So if the code was YGOY

and a user guessed BYOY

it should output:

2 correct
1 almost

What I am having difficulty with is that I am using these characters 'O', 'G', etc and trying to compare them in my function comparearray to see if they are in the right location. It isn't working and my compiler is warning me that I am comparing an integer with a pointer. I don't think I am doing this because I am simply comparing the guess that the user inputs with the code that is generated.

5. Originally Posted by stahta01
Random guess

?Wrong code?
Code:
`if(codearr[i] == guess_array[i])`
Maybe right
Code:
`if(codearr[i] == guess_array[i][j])`
I picked j as the second index at random; you might or should use another variable.

Tim S.
Thanks Tim

That helps with my compiler not driving me crazy. Now I have to find out why it is still not working.