Я пытался использовать как бинарный поиск, так и циклы while и for в моих поисках, и возникает та же проблема.Когда моя исходная программа приходит к этому вызову функции, функция линейного поиска (displayContent) всегда назначает -1 для позиции, и после вызова функции программа прерывается и завершается.Я попытался переставить свою программу, как я сказал, что я пробовал циклы и циклы while с двоичным и линейным поиском.
Я также использую структурный тип данных
struct info
{
string name;
double score[5];
double avg;
};
Вот мой вызов функции
cout<<"Please enter the name of the person which you would like to search. ";
getline(cin, name);
cin.ignore();
displayContent(contestant, count, name);
Вот мое определение функции
void displayContent(info contest[], int quantity, string id)
{
int position=-1;
bool found=false;
for(int index=0;index<quantity && !found;index++)
{
if(contest[index].name.compare(id)==0)
{
found=true;
position=index;
}
}
if(position==-1)
{
cout<<"That person was not one of the contestants.";
}
else
{
cout<<"The scores for "<<contest[position].name<<" are \n Contestant Judge1 Judge2 Judge3 Judge4 Judge5 Average"
<<"\n______________________________________________________________________"<<endl;
cout<<right<<setw(15)<<fixed<<setprecision(1) <<contest[position].name<<setw(10)<<contest[position].score[0]<<setw(8)<<contest[position].score[1]<<setw(8)<<contest[position].score[2]<<setw(8)<<contest[position].score[3]
<<setw(8)<<contest[position].score[4]<<setw(8)<<contest[position].avg<<endl;
}
}