Thread: Comparing Pointer and Integers?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    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");
        printf("What's your curent guess?\n ");
        generate_code(codearr);
        printf("%s", codearr);
    
        //while (answer == 0)
        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. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    your description isn't clear. where exactly is the problem? be more specific

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Quote Originally Posted by dmh2000 View Post
    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. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Quote Originally Posted by stahta01 View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing integers
    By tosihiro2007 in forum C Programming
    Replies: 2
    Last Post: 03-15-2013, 01:30 PM
  2. comparing 2 strings with pointer
    By meriororen in forum C Programming
    Replies: 9
    Last Post: 05-22-2009, 07:37 PM
  3. Comparing two sets of integers
    By bertazoid in forum C Programming
    Replies: 10
    Last Post: 11-26-2008, 07:37 AM
  4. Comparing Pointer types in C
    By ladesidude in forum C Programming
    Replies: 16
    Last Post: 07-15-2008, 09:34 PM
  5. Comparing integers
    By jw232 in forum C++ Programming
    Replies: 17
    Last Post: 04-01-2008, 11:36 AM