Binary Search not working

whatever number I type in it always goes to element 0 and it always says the number is valid. Been messing around with this and reading the code for a couple hours now and CANNOT figure out why.
help please. thank you

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>

using namespace std;

int binarySearch(int[], int, int);
void binarySort(int[], int);

// , 11/26/2016, PROGRAM CHALLENGE 4: CHARGE ACCOUNT VALIDATION MODIFICATION

int main()
{
	int num, account;
	// ACCOUNT NUMBERS USING SINGLE DIMENSION ARRAY
	int accountNum[18] = { 5658845, 4520125, 7895112, 8777541, 8451277, 1302850,
						8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
						1005231, 6545231, 3852085, 7576651, 7881200, 4581002 };
	for (int i = 0; i < 18; i++)
	{
		cout << accountNum[i] << endl;
	}
	cout << "Enter account number you are searching for: ";
	cin >> num;
	// CALL FUNCTION TO SORT ACCOUNT NUMBERS FOR BINARY SEARCH
	binarySort(accountNum, 18);
	for (int i = 0; i < 18; i++)

	// CALL FUNCTION TO SEARCH FOR ACCOUNT USING BINARY SEARCH
	account = binarySearch(accountNum, 18, num);

	// DISPLAY WHETHER ACCOUNT NUMBER IS VALID OR INVALID
	if (account == -1)
		cout << "Account is invalid." << endl;
	else
		cout << "Account number " << accountNum[account] << " is valid." << endl;

	system("pause");
	return 0;
}

// FUNCTION FOR USING BINARY SEARCH FOR ACCOUNT NUMBER
int binarySearch(int array[], int SIZE, int searchNum)
{
	bool found = false;
	int mid, first, last;
	first = 0;
	last = (SIZE - 1);
	while ( found == false && first <= last)
	{
		mid = (first + last) / 2;
		if (array[mid] == searchNum)
		{
			found = true;
			return mid;
		}
		else if (array[mid] > searchNum)
			last = (mid - 1);
		else
			first = (mid + 1);
	}
	return 5;
}

void binarySort(int array[], int SIZE)
{
	int minValue, minIndex;
	for (int scan = 0; scan < (SIZE - 1); scan++)
	{
		minIndex = scan;
		minValue = array[scan];
		for (int i = scan + 1; i < SIZE; i++)
		{
			if (array[i] = minValue)
			{
				minValue = array[i];
				minIndex = i;
			}
		}
		array[minIndex] = array[scan];
		array[scan] = minValue;
	}
}
If you pay attention to the warnings generated by your compiler, you probably get a message about line 72, which should be using == for comparison instead of = for assignment.
The flag 'found' is useless since you return after setting it to true, so it will never be true while the loop is running, and you ignore it after the loop. Also, the account is never invalid because binarySearch can never return -1, on a fail it returns 5, which is a "valid" 6th element. What's that empty for loop on line 25? Have you checked the result of the sort function? It may be the key, since the entire binary search algorithm depends on it.
ugh, wow. such a simple mistake. thank you cire.
and all the weird "return 5;" and incomplete for loop stuff was stuff i forgot to take out before posting this, just random things trying to figure out why this code wasnt working.
Topic archived. No new replies allowed.