I've written a program for class that is a guessing game. The user inputs a number, and the number is compared to an existing array. The only problem is, my function that compares the user's number to the array is always returning true, and I can't seem to figure out why. Here's the relevant code:
bool searchNumber (int array [], int size, int number)
{
bool truth=false;
for (int i; i<=size-1; i++)
{
if (number=array[i])
{
return truth=true;
}
}
}
is incorrect.
Firstly, you did not initialize 'i' inside the control statement of the loop. Further instead of comparing ( == ) you assign ( = )to number the value of array[i]
if (number=array[i])
The correct code wiil look the following way
1 2 3 4 5 6 7 8 9 10 11 12
bool searchNumber ( constint array [], int size, int number )
{
for ( int i = 0 ; i < size; i++ )
{
if ( number == array[i] )
{
return ( true );
}
}
return ( false );
}
My compiler didn't like that at all. It gave me this:
Undefined symbols for architecture x86_64:
"searchNumber(int*, int, int)", referenced from:
_main in cc7tLYDa.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status