Hello FanOfThe49ers,
My apologies. Sometimes I forget that in the utopia of school no one does anything wrong.
The part about listing the available file names is optional. You do not have to use it.
when i run my program it runs fine |
This may be true for you, but not always true for others.
Just because your code may seem to work does not mean that it is correct.
In your original code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
if (inputFile)
{
// Read the numbers from the file and display them.
while (count < ARRAY_SIZE && inputFile >> numbers[count])
count++;
{
cout << number << endl;
}
// Close the file
inputFile.close();
}
else
{
// display an error message if the file was not found
cout << "Error opening the file.\n";
inputFile.close();
}
|
Lines 6, 7 and 8 the {}s do not make this part of the while loop. The is a separate block. The while loop ends when it finds the first (;).
The else statement prints an error message, providing that the if statement is false, closed a file stream that is not open and continues on with the program. This is not what you want.
If you open the file and the if condition is true the while loop properly reads the file and the else is bypassed and your program continues.
Next you display the numbers before they are sorted. That is fine, but not what the "binarySearch()" need to work with.
The "binarySearch()" needs a sorted array to work properly. If you sort the array before you call the "binarySearch()" both functions work fine.
Your code
int binarySearch(const int array[], int size, int value);
your line 68 is a very nice prototype or function definition, but you already have both. What it is not is a function call. The function call should be
position = binarySearch(numbers, count, number);
.
"position" captures the return value of the function and all you need to call the function is the names that were created in "main". Changing the names at the function is OK.
When you run your for loop:
1 2 3 4 5 6
|
for (int idx = 0; idx < count; idx++)
{
cout << numbers[idx] << " ";
}
cout << endl;
|
This will print 1 vary long line, but your console window will wrap the line when it reaches the right side. When posting that output here it shows up as 1 vaer long line because that is what it is. It might wrap the line is you omit the output tag. Not sure.
At the end of "main" I did make this change:
1 2 3 4 5 6 7 8 9 10 11
|
if (number == 0)
cout << "\nYou have chosen to exit the program.\n";
else if (position > 0)
{
cout << "\nThat number was found at element " << position;
cout << " in the array\n";
}
else
{
std::cout << "\n Number not found.\n";
}
|
The problem here is that if number == 0 you are checking for this after you have sorted and searched for (0). This check needs to be done after you enter the number to search for. Then the "else if" would change to just an "if" statement.
Your are correct in saying that your code works, just not working in the best way.
If you can get what you have working better at least 1 time then you can add
ne555's idea of the while loop. By then it should be easier to make that change.
Andy