### Binary search of string in array of objects

I need to search for a string in an array of objects, this is what I have but it does not seem to work, it always gives me the second string in the array instead of the one that i search for.

 ``123456789101112131415161718192021222324252627282930`` ``````void binarySearch(Student S[], string name) { int first = 0; int last = 9; int middle; int position = -1; bool found = false; while(!found && first name) { last = middle -1; } else { first = middle +1; } } cout<
This looks like binary search - is your input array sorted?
yes it is sorted, here is the code as it looks when sorted...

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````void binarySearch(Student S[], string name) { int first = 0; int last = 9; int middle; int position = -1; bool found = false; //FIRST SORT THE OBJECTS SO THAT THEY CAN BE SEARCHED Student temp; bool swap; do { swap = false; for (int i = 0; i<(10-1);i++) { if(S[i].getName() > S[i+1].getName()) { temp = S[i]; S[i] = S[i+1]; S[i +1] = temp; swap = true; } } }while(swap); cout<<"Name Age Average"< name) { last = middle -1; } else { first = middle +1; } } cout<<"Name Age Average"<

the sorting part works well
is it possible that middle should be a double instead of an int? since it derives from a division and therefore it contains decimal points.
Topic archived. No new replies allowed.