Thread: guessing game

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    guessing game

    I am very new at C , Any help would be greatly appreciated
    This function is made to check the 2d array if guess matches num and loop untill the user enters -1.
    the function call is not the problem its the return.

    Code:
    int Find(int a[][SIZE],int num)
    {
      int y;
      int x;
      int guess=0;
    
      while(num != -1)
        {
    
          for(x=0;x<SIZE;x++)/*loop 1*/
            {
              for(y=0;y<SIZE;y++)/*loop 2*/
                {
                  guess = a[x][y];
                  if (num == guess )
                    {
                      return 1;
                      break;
                    }
                  else
                    {
                      printf("Keep Guessing:");
                      scanf("%d",&num);
                    }
                }/* close loop 2*/
            }/* close loop 1 */
        }/*while */
      return 0;
     
     }/*Find*/

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, two things i would suggest: make your loop a do..while rather then a while loop. the break statement after the return 1 will never get executed so remove it.

    the function call is not the problem its the return.
    without knowing the problem, where do i start looking? what is the situation with the return?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    it just keeps going diplay keep guessing over and over endless.
    how could i break out of the while if the guess is right.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    can you post your full code so we can see how its being called and what variables are what when passed.

    i was trying to understand what the parameters were... the function is to find a number in a 2-dimensional array right? with the 2nd parameter being an initial guess? also this is an A x A (square) array right, so why not use a[SIZE][SIZE] in the parameter list?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Code:
                                                                                                                                                                             
    #define SIZE 4
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
      int table[SIZE][SIZE];
      int Find(int a[][SIZE],int);
      int num;
      int find;
      int x,y;
      srand(time(NULL));
      for(x=0;x<SIZE;x++) /* loop 1 */
        {
          for(y=0;y<SIZE;y++) /* loop 2 */
            {
              table[x][y]=100+rand()&#37;101;
            } /* close loop 2 */
        } /* close loop 1 */
      printf("enter a number\n");
      scanf("%d",&num);
      find = Find(table,num);
      if(find==1)
        {
           printf("good job you win");
        }
      else
        {
          printf("sorry you lost");
        }
    
    }
    int Find(int a[][SIZE],int num)
    {
      int y;
      int x;
      int guess=0;
    
      while(num != -1)
    {
    
          for(x=0;x<SIZE;x++)/*loop 1*/
            {
              for(y=0;y<SIZE;y++)/*loop 2*/
                {
                  guess = a[x][y];
                  if (num == guess )
                    {
                      return 1;
                      break;
                    }
                  else
                    {
                      printf("Keep Guessing:");
                      scanf("%d",&num);
                    }
                }/* close loop 2*/
            }/* close loop 1 */
        }/*while */
      return 0;
    
    }/*Find*/
    this is the rest of my code
    Last edited by Sal79; 05-08-2007 at 08:01 PM. Reason: wrong file sorry

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    thats not the rest of your code.. if it is, then you cant get it to even compile (which you said you can, therefore this isnt all of your code).

    please just open your file, select all, and copy then paste here.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Sorry i had copied the wrong file
    it just loops endlessly

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    All I can think of is to add some temporary printf statements to check to see if it's getting to some parts of your loop. As a side note, a break in an inner loop will not cause the outer loop to also break, but seeing a "return" in there, it would just terminate the whole function whether or not the loops' conditions are true or false.

    Better indentation can also be helpful.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im still waiting for the answers from post #4 in this thread.

    if the function is supposed to search through the entire array for the number, it isnt working. for the first iteration it compares the number given by the user with table[0][0]. if they arent equal it will read another number, and compare that to table[0][1], and so on. is this what you want?

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    It should? go through table[0][0] table[0][1] table[0][2] table[0][3]
    table[1][0] table[1][1] table[1][2] table[1][3]
    table[2][0] table[2][1] table[2][2] table[2][3]
    table[3][0] table[3][1] table[3][2] table[3][3]
    and check to see if num = said table in the nested for loop.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you want to guess a number and check the entire array for at least one occurance of it, you need to rethink your loops and move stuff around.. go through on paper what is happening in your Find function and you will see your simple mistake.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Code:
    int Find(int a[][SIZE],int num)
    {
      int y;
      int x;
      int guess=0;
    
      do
        {
    
          for(x=0;x<SIZE;x++)/*loop 1*/
            {
              for(y=0;y<SIZE;y++)/*loop 2*/
                {
                  guess = a[x][y];
                  printf("&#37;d",a[x+1][y]);
                  if (num != guess )
                    {
                      printf("Keep on Guessing:");
                      scanf("%d",&num);
                    }
                  else
                    {
                      return 1;
    
                    }
                }/* close loop 2*/
            }/* close loop 1 */
       }while (num!=guess||num!=-1);/*while */
      return 0;
    
    }/*Find*/
    this is some things i have changed. I hope i just didn't make something simple much harder
    Last edited by Sal79; 05-08-2007 at 09:46 PM. Reason: spelling

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i was thinking something more like:
    Code:
    int Find(int a[][SIZE],int num)
    {
      int y;
      int x;
      int guess=0;
    
      while (1)
        {
         printf("Guess a number:");
         scanf("&#37;d",&num);
    
          if (num == -1)
                    return 0;
    
          for(x=0;x<SIZE;x++)/*loop 1*/
            {
              for(y=0;y<SIZE;y++)/*loop 2*/
                {
                  guess = a[x][y];
    
                  if (num == guess )
                      return 1;
                }/* close loop 2*/
            }/* close loop 1 */
       }/*do */
    }/*Find*/
    note: i havent tested this
    Last edited by nadroj; 05-08-2007 at 11:26 PM.

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    As a side note, why are you returning the value of 1 to break to the loop? That is generally used as an error detection value. Return 0 if possible if you are returning a value to the OS, and use return 1 for error value returns for say, if you cant open a file.
    Double Helix STL

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    I would like to take the time to thank everyone who posted on this thread.
    It was of great help to me !!!!
    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 03:05 AM
  5. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM