I seem to be having a problem reading in an array from a .txt file. I am looking to use a binary search to search an array so that the position of the number entered is outputted.
Whenever I run the program it will not read in any of the array numbers in the .txt file, but instead it will repeat the following:
Enter a number to find in the array: Entered Number
The number you entered does not exist in the array.
It seems that the problem is that the array from the .txt file is not being read whenever the code is compiled.
#include<iostream>
#include<string>
#include<fstream>
usingnamespace std;
int binarySearch(constint [], int, int);
constint SIZE = 99;
int main()
{
ifstream file("Numbers.txt");
if(file.is_open())
{
string myArray[99];
for(int i = 0; i < 99; ++i)
{
file >> myArray[i];
}
}
int myArray[SIZE];
int results;
int arryNum;
do
{
cout<<"Enter a number to find in the array: ";
cin>>arryNum;
results = binarySearch(myArray, SIZE, arryNum);
if (results == -1)
cout<<"The number you entered does not exist in the array. \n";
else
{
cout<<"Found @ literal position(s): "<<results<<endl;
cout<<"To quit enter 1000"<<endl;
}
}while(arryNum != 1000);
return 0;
}
int binarySearch(constint array[], int size, int value)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while(!found && first <= last)
{
middle = (first + last) / 2;
if (array[middle] == value)
{
found = true;
position = middle;
}
elseif (array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
Your use of the if statement is common with new programmers. It does work, but as in this case it is giving you a problem.
I have found this to be a better way of dealing with a file stream:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
int myArray[SIZE]{};
ifstream file("Numbers.txt");
if (!file)
{
std::cout << "File \"Numbers.txt\' did not open" << std::endl;
return 1;
}
for (int i = 0; i < SIZE; ++i)
{
file >> myArray[i];
}
You have defined the constant "SIZE", so make use of it.
The for loop works as long as you have 99 numbers in the file. If by chance there is less than 99 numbers the for loop would continue until "i" equals 99 and the test fails leaving you to process a file stream that has failed and looping for no reason. Something you could try, and I have not tested this yet, is: for (int i = 0; i < SIZE && file >> myArray[i]; ++i); which will eliminate the need for the block. And if you have less than 99 numbers the for loop will end when the file stream reaches end of file or "i" becomes 99 which ever comes first.
Another alternative is: while (i < SIZE && file >> myarray[i++]);. It works the same as the for loop. In future programs you might use this as:
The way this works is as long as the while condition can read something you keep looping. When the read tries to read past end of file the stream fails and so does the while condition.
Something that occurred to me while testing your program. This is a case where "i" is better defined outside of the for loop. This way when the for loop ends "i" will have a value of how many numbers have been read. This can be used later in the program to only process the part of the array that is used should there be less than 99 numbers.
Also instead of using "i" a better name would be "count" or "numbersRead". Something that has a better meaning than "i".
BTW using what you started with the for loop would now look like: for (i = 0; i < SIZE && file >> myArray[i]; ++i);
And I also changed this: int myArray[SIZE]{}, i{};