### Determining truth

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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include using namespace std; // Function prototypes int findSmallest (int [], int); int findLargest (int [], int); bool searchNumber (int [], int, int); int main () { // Define variables char choice; bool truth; int smallest, largest, number; const int SIZE=100; int index=0; int array [SIZE]; // Open file ifstream inputFile; inputFile.open ("numbersToGuess.txt"); // Read file to array while (index < SIZE && inputFile >> array [index]) { index++; } // Call functions smallest = findSmallest (array, index); largest = findLargest (array, index); do { cout << "Guessing Game" << endl << "+++++++++++++" << endl; cout << "Pick a number between " << smallest << " and " << largest << ": " << endl; cin >> number; // Input validation while (number < smallest || number > largest) { cout << "Guess out of range. Try again." << endl; cin >> number; } truth=searchNumber(array, SIZE, number); if (truth=true) { cout << "Your pick was: Correct" << endl; } else { cout << "Your pick was: Incorrect" << endl; } cout << "Enter Y or y to repeat: "; cin >> choice; } while (choice == 'Y' || choice == 'y'); return 0; } 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; } } }``````

Thank you!
Last edited on

 ``1234567891011`` ``````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

 ``123456789101112`` ``````bool searchNumber ( const int array [], int size, int number ) { for ( int i = 0 ; i < size; i++ ) { if ( number == array[i] ) { return ( true ); } } return ( false ); }``````

Last edited on
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
collect2: ld returned 1 exit status
I wrote the following declaration

bool searchNumber ( const int array [], int size, int number )

where the first parameter has const qualifier. So update the declaration which is before main
Last edited on
It compiles now, but it still says everything is true.
Here

if (truth=true)

again instead of comparing ( == ) you assign true.
It is simply to write

if ( truth )
It works now! Thank you so much. I really appreciate it.
Topic archived. No new replies allowed.