# guessing game

• 05-08-2007
Sal79
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*/```
• 05-08-2007
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.

Quote:

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?
• 05-08-2007
Sal79
it just keeps going diplay keep guessing over and over endless.
how could i break out of the while if the guess is right.
• 05-08-2007
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?
• 05-08-2007
Sal79
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
• 05-08-2007
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.
• 05-08-2007
Sal79
Sorry i had copied the wrong file
it just loops endlessly
• 05-08-2007
ulillillia
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.
• 05-08-2007
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?
• 05-08-2007
Sal79
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.
• 05-08-2007
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.
• 05-08-2007
Sal79
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
• 05-08-2007
```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*/```