I can't understand why user_input isn't being reassigned every time the function is recalled...
Code:
int search(int population[], int i)
{
int cell_number;
int user_input;
char continue_function[5];
printf("Enter some integer:");
scanf("%d", &user_input);
int j;
while (j<i)
{
if(user_input == population[j])
{
cell_number = j;
printf("the user input is %d.\n", user_input);
printf("j is %d\n", j);
printf("The number that you entered is in cell number %d\n", cell_number);
printf("Would you like to continue? ");
scanf("%s", continue_function);
if ( strcmp(continue_function, "yes") == 0)
{
user_input = 0;
printf("Trigger.\n");
int j = 0;
printf("j is %d\n", j);
search(population, i);
}
if (strcmp(continue_function, "no") == 0)
{
j=i;
printf("j is %d\n", j);
search(population, i);
}
/*
I figured out the problem. It's because we initialized j in the beginning of the program.
Every time we recall the function, it resets j to zero. So, anything that we had previously is erased...
But if we don't initialize it, the first value that we put in is stuck...
*/
}
if (user_input != population[j])
{
j++;
}
if(j==(i))
{
printf("That number is not in the array.\n");
printf("Would you like to continue? ");
scanf("%s", continue_function);
if (strcmp(continue_function, "yes"))
search(population, i);
if(strcmp(continue_function, "no"))
j=i;
}
}
return 0;
}
Thanks for the help.