im trying to perform an index search for string, it runs but the problem is that it only prints out i[0] which is my first entry. if i lookup another entry it doesn’t work. Please help..
void clist(string fn[],string ln[], int size);
int search_list(const string fn[],const string ln[], int size, string find);
int main(){
string search;
cout << "This program searches a list .\n";
const int total = 3;
string fn[total];
string ln[total];
clist(fn,ln, total);
cout << "Search contact:____ ";
cin >> search;
search_list(fn,ln, total, search);
return 0;
}
void clist(string fn[],string ln[], int size){
cout << "Enter " << size << " contact.\n";
for (int index = 0; index < size; index++)
cin >> fn[index] >> ln[index] ;
}
int search_list(const string fn[], const string ln[],int size, string search){
for(int i=0;i<size;i++){
if((fn[i] == search)&& (i < size)){
cout<<"Result found "<<fn[i]<<" "<<ln[i]<<endl;
break;
}
cout<<"no record found"<<endl;
break;
}
}
You are making a loop and explicitly telling it to
breakout of it after the first iteration. Try writting it like this: