Two questions.
Is the file data in ascending order?
What is that for loop inside the first if of your binary search supposed to be doing? If you found that array[middle] is the value you seek, return middle, right there and then. That for loop just spins around a lot, and since s1 and s2 were never initialized, they probably will match as empty strings.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
jencas - good catch. I was just looking at the sort function.
meistrizy - you need a function that reads the data from the file into your names[] array. It ought to return the number of names actually read in, which should be used when passing the array to the sort, rather than using the size of the array. There might not be 20 names in the file.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
read and heed your compiler warnings.
This line
else
{
for (int i; i < s; i++) // THIS ONE
getline(dataIn, array[i]);
uses an uninitialized value for i - so where's your data going?
Then, I would suggest you really look at your structure - it doesn't make a lot of sense.
Each function should do one thing, and one thing only. Your binsearch( ) gets the search target, then calls selectsort(), which does the actual program input?! before sorting the data.
Break these out so they are independent of each other. Sort sorts, search searches, input should be a separate function. Let main ask the user what to look for after data has been read and sorted. And don't assume that just because your data array is size 20 that you will always get exactly 20 data values from the file.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Thanks for your input and suggestions vmanes. My code works- I found that the problem was in my if/else statement. Results should == -1 indtead of results = -1.
Well, there is that problem as well. Glad you got it all going.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228