1. ## 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. 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. 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. 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. 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

6. 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. Sorry i had copied the wrong file
it just loops endlessly

8. 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. 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. 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. 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. 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

13. 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

14. 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.

15. I would like to take the time to thank everyone who posted on this thread.
It was of great help to me !!!!
thanks again