String Search

Hey guys hows it going, back with another one that is mostly done. Getting a error code on this program:
main.cpp:30:49: error: too many arguments to function 'int binarySearch(std::string*, int)'


Any help would be great
_________________________________


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void sortStrings(string[],int);
int binarySearch(string[],int);
const int Size = 20;

int main(){

int results;
string employeeName;

string emps [Size]= {
"Collins, Bill", "Smith, Bart", "Allen, Jim",
"Griffin, Jim", "Stamey, Marty", "Rose, Geri",
"Taylor, Terri", "Johnson, Jill",
"Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
"James, Jean", "Weaver, Jim", "Pore, Bob",
"Rutherford, Greg", "Javens, Renee",
"Harrison, Rose", "Setzer, Cathy",
"Pike, Gordon", "Holland, Beth" };


cout << " Enter name to be searched" << endl;
cin >> employeeName;

sortStrings(emps,Size);
results = binarySearch(emps, Size, employeeName);

if(results==-1)
cout << employeeName << " Name not found " << endl;

else
cout << " Name " << employeeName << " found in index " << results+1 << endl;
cout << "\nElement names list is: " << endl;

for(int index=0;index,Size;index++)
cout << index+1 << right << setw(10) << emps[index] << endl;

return (0);
}

void sortStrings(string names[], int size){
int startScan;
int minIndex;
string minValue;

for(startScan=0; startScan<(size-1);startScan++)
{
minValue=startScan;
minValue=names[startScan];
for(int index=startScan+1;index<size;index++)
{
if(names[index].compare(minValue)<0){
minValue=names[index];
minIndex=index;
}

}
names[minIndex]=names[startScan];
names[startScan]=minValue;
}
}

int binarySearch(string emps[],int Size, string employeeName)
{
int first=0,
last = Size-1,
middle,
position=-1;
bool found = false;

while(!found && first <=last)
{
middle = (first+last)/2;
if(emps[middle].compare(employeeName)==0)
{
found=true;
position=middle;
}

else if(emps[middle].compare(employeeName)>0)
last=middle-1;

else
first=middle+1;
}
}



@drewdizzle92,

The error message is fairly explanatory.
error: too many arguments to function 'int binarySearch(std::string*, int)'



You have a function prototype or declaration with TWO arguments.
int binarySearch(string[],int);
so that is what your code is expecting to call.

You subsequently call it (and later define it) with THREE arguments
1
2
3
results = binarySearch(emps, Size, employeeName);
...
int binarySearch(string emps[],int Size, string employeeName)



Your error message even tells you (one of) the problem lines!


I should fix the prototype.
Last edited on
your function declaration: int binarySearch(string[],int); //2arguments
your function definition:
1
2
int binarySearch(string emps[],int Size, string employeeName)
{/*[...]*/}  //3 arguments 

your function call in main: results = binarySearch(emps, Size, employeeName); // 3 arguments

It seems you forgot to include your third argument in your function declaration.
try: int binarySearch(string[],int, string);
Last edited on
LOL, noob move forsure.
Thanks guys, im gonna go ahead and blame that on the time
Hmmm, Now im getting a segmentation fault. Anyone see why?
The program runs, just doesnt seem to be running properly
No worries!
@Drewdizzle,

Please
(1) Put your code in code tags - it is unreadable without indentations.

(2) Do ONE thing at a time. Remove the binary search completely until you have got the sorting to work. (It is currently wrong).

(3) Turn error checking on when you compile. At the very least,
-Wall -pedantic -Wextra
or something similar. This will show up quite a few problems in this instance - all easy for you to fix.


Your sorting is wrong.
Do these lines not strike you as odd?
1
2
minValue=startScan;
minValue=names[startScan];



Also have another think about
for(int index=0;index,Size;index++)


Do not proceed further until you have fixed your sort.

When you have fixed that, turn the error checking up so that you can debug your binary search.

Last edited on
Your binarySearch needs to return a value.
There are other flaws but one step at a time.

If this isn't homework it would be much easier to use the standard C++ facilities.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>

using namespace std;

int main() 
{
  vector<string> emps = 
  {
    "Collins, Bill", "Smith, Bart", "Allen, Jim",
    "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
    "Taylor, Terri", "Johnson, Jill",
    "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
    "James, Jean", "Weaver, Jim", "Pore, Bob",
    "Rutherford, Greg", "Javens, Renee",
    "Harrison, Rose", "Setzer, Cathy",
    "Pike, Gordon", "Holland, Beth" 
  };
  string employeeName;
  cout << " Enter name to be searched" << endl;
  getline(cin, employeeName);

  sort(emps.begin(), emps.end());
  bool found = binary_search(emps.begin(), emps.end(), employeeName);

  if (found)
    cout << employeeName << "Found it" << endl;
  else
    cout << "Not found" << '\n';
  
  cout << "\nElement names list is: " << endl;
  for (size_t index = 0; index < emps.size(); index++)
    cout << index + 1 << '\t' << right << setw(10) << emps[index] << endl;
}
Unfortunately this is homework.The problem is :

Modify the binarySearch function presented in this chapter so it searches an array
of strings instead of an array of ints. Test the function with a driver program. Use
Program 8-8as a skeleton to complete. (The array must be sorted before the binary
search will work.)
8-8 was just the list of names and a small portion of main.

Ill have to take a look at it again later as I have work today
Topic archived. No new replies allowed.