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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
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
Your function searchNumber

1
2
3
4
5
6
7
8
9
10
11
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 ( 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
ld: symbol(s) not found for architecture x86_64
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.