#include <iostream>
using namespace std;
int binarySearch(const int [], int , int );
const int size = 2;
struct Staff
{
char name[30];
int ID;
char posit[15];
float salary;
char department[10];
};
int main()
{
int result,ID;
Staff Info[size];
for(int i=0; i<size; i++)
{
cout<<"Staff's name : ";
cin>>Info[i].name;
cout<<"\nStaff's ID : ";
cin>>Info[i].ID;
cout<<"\nStaff's position : ";
cin>>Info[i].posit;
cout<<"\nStaff's salary : ";
cin>>Info[i].salary;
cout<<"\nStaff's department : ";
cin>>Info[i].department;
}
for(int i=0; i<size; i++)
{
cout<<"Staff "<<i+1<<endl;
cout<<"Staff's name : "<<Info[i].name;
cout<<"\nStaff's ID : "<<Info[i].ID;
cout<<"\nStaff's position : "<<Info[i].posit;
cout<<"\nStaff's salary : "<<Info[i].salary;
cout<<"\nStaff's department : "<<Info[i].department;
}
cout<<"Enter the Staff's ID you wish to search : ";
cin>>ID;
result =binarySearch(Info, size, ID);
if(result==-1)
cout<<"Not found!"<<endl;
else
{
cout<<"Staff's name : "<<Info[size].name;
cout<<"\nStaff's ID : "<<Info[size].ID;
cout<<"\nStaff's position : "<<Info[size].posit;
cout<<"\nStaff's salary : "<<Info[size].salary;
cout<<"\nStaff's department : "<<Info[size].department;
}
return 0;
}
int binarySearch(int 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;
}
else if(array[middle]>value)
last=middle-1;
else
first=middle+1;
}
return position;
}
Error show:
error C2664: 'binarySearch' : cannot convert parameter 1 from 'Staff [2]' to 'const int []'
It's exactly what the error says, you can't convert from a Staff array to an integer array.
so how to solve this problem. Got another way of writing this coding?