| | |
Binary Search Array From A File Help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 0
Thanks in advance for your help. The problem is this: I am supposed to read an array from a file and ask the user to input a name to search for within the file. If the name is there then return the position number in the file, if not output an error message. I get my program to read the file but when it searches for the name it always comes back false. What am I missing? Here is my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; int binarySearch(string[], int, string); const int SIZE = 20; int main() { //define array and input string names[SIZE]; string peopleSearch; int results; char ag; ifstream dataIn; dataIn.open("sortedNames.txt"); do { //get user data cout << "Enter the name of the person you would like to search for (Last, First): \n"; cin.ignore(); getline(cin, peopleSearch); cout << endl; //search for the person results = binarySearch(names, SIZE, peopleSearch); //return -1 if person not found if (results = -1) { cout << "That person does not exist in this array. You might try \n"; cout << "www.randyspeoplesearch.com. That is a great site for finding people."; cout << endl; cout << endl; } else { //Otherwise results contain info cout << "Congratulations! You have found " << peopleSearch << " in the array \n"; cout << "in position " << results << "of the array."; cout << endl; cout << endl; } cout << "Would you like to run another person search? Y/N: "; cin >> ag; cout << endl; }while (ag == 'y' || ag == 'Y'); dataIn.close(); system("pause"); return 0; } int binarySearch(string array[], int size, string value) { int first, last = size - 1, middle, position = -1; bool found = false; string s1, s2; int index; int pos; while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) { for(int i = 0; i < size; i++) { for(int h = 0; h < size; h++) { if (s1 == s2) { found = true; position = middle; } } } } else if (array[middle] > value) last = middle - 1; else first = middle + 1; } return position; }
Last edited by meistrizy; Mar 5th, 2009 at 11:03 pm.
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.
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.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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.
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.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Dec 2008
Posts: 26
Reputation:
Solved Threads: 0
Well...I modified my program but it still isn't reading the names in properly. It will decide whether or not it is true or false but not based on the input from the file. Since it is not reading the names in right it only spits out the last position. ???
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; void selectSort(string[], int); void binSearch (string[], int); int main() { const int size = 20; string array[size]; int results; char ag; string peeps; binSearch(array, size); system ("pause"); return 0; } void selectSort(string array[], int s) { int startScan, minIndex; string minValue; string peopleSearch; ifstream dataIn; dataIn.open("names.txt"); if (!dataIn) cout << "There was an error opening up this file."; else { for (int i; i < s; i++) getline(dataIn, array[i]); for (startScan = 0; startScan < (s - 1); startScan++) { minIndex = startScan; minValue = array[startScan]; for (int index = startScan + 1; index < s; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } dataIn.close(); } void binSearch(string array[], int s) { string peeps; int first = 0, last = s - 1, middle, position = 0; bool found = false; cout << "Enter a name you would like to search for: \n"; getline(cin, peeps); cout << endl; selectSort(array, s); while (first <= last && !found) { middle = (first + last) / 2; if (array[middle] == peeps) { found = true; position = middle; } else if (array[middle] > peeps) { last = middle - 1; position = last; } else { first = middle + 1; position = first; } } if (found) { cout << endl; cout << "That person does not exist in this array. You might try \n"; cout << "www.randyspeoplesearch.com. That is a great site for finding people."; cout << endl; cout << endl; } else { cout << endl; cout << "Congratulations! You have found " << peeps << " in the array \n"; cout << "in position " << position << " of the array."; cout << endl; cout << endl; } }
read and heed your compiler warnings.
This line 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.
This line
C++ Syntax (Toggle Plain Text)
else { for (int i; i < s; i++) // THIS ONE getline(dataIn, array[i]);
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.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Great Binary Search Program, Unable to Output to File. Please Help! (C++)
- Problem with Binary Search (C++)
- Pascal - student search program (Pascal and Delphi)
- Binary search from file .. Help (C++)
- contact info in array of structs (C++)
- C++ 6. Search Benchmarks. (C++)
- Binary Search on a text file (Java)
Other Threads in the C++ Forum
- Previous Thread: Wallis' Product (infinite series)
- Next Thread: C++ and GRaphs
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






