0

検索でバイナリ検索と while ループと for ループの両方を使用してみましたが、同じ問題が発生しています。

私の元のプログラムがこの関数呼び出しに来ると、線形検索関数 (displayContent) は常に位置に -1 を割り当て、関数呼び出しの後、プログラムは中断して終了します。

私は自分のプログラムを再編成しようとしました。私が言ったように、二分探索と線形探索の両方で for ループと 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;
    }
}
4

1 に答える 1

1

getlineあなたはそれがあなたが期待することをすることを確認しましたか?おそらくname、行末文字が含まれています。入力の問題を除外するには、を呼び出す前にname存在することがわかっている値を割り当ててみてください。contestantdisplayContent

検索アルゴリズムの問​​題を見つけることができませんでした。

于 2011-05-03T08:52:08.267 に答える