在C ++中如何组合搜索功能和切换,这样交换机可以要求用户从搜索结果中选择并显示结果信息?

时间:2016-12-04 22:01:56

标签: loops search switch-statement

  1. 代码要求用户从团队中挑选一名玩家,并且应该显示玩家统计信息。搜索也必须能够进行部分输入搜索。这是我到目前为止提出的代码。但我卡在交换机正常工作或搜索功能之间,但无法使它们一起工作。谢谢你的帮助。

    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    int index = 0;
    int choice;
    int counter = 1;
    string player, result;
    
    
    cout << "\n\nThis program will ask the user for a player name from\n";
    cout << "the team Dallas Mavericks and give the statistics of the player.\n";
    cout << "  *Last Name First\n";
    cout << "  *Seperated By Comma\n";
    cout << "  *Use correct capitalization\n";
    cout << "Please enter player's name: ";
    getline(cin, player);
    
    ifstream inputFile;
    inputFile.open("statisticsMavs.txt");
    if (inputFile.fail())
    {
        cout << "Could not open file";
        return 1;
    }
    
    ofstream outputFile;
    outputFile.open("playerStats.txt");
    
    if (outputFile.fail())
    {
        cout << "Could not open file";
        return 1;
    }
    
    for (index = 0; getline(inputFile, result); index++) 
    {
        if (result.find(player) != string::npos)
        {
            cout << endl << counter++ << "  " << result.substr(0, 20) << endl;
            continue;
        }
    
        else
        {
            continue;
            cout << "Could not find player! Please enter another name: ";
            getline(cin, player);
        }
    }
    
    
    cout << "Please choose the number from the correct search result: ";
    cin >> choice;
    switch (choice)
    {
    case 1: outputFile << result << endl;
        return 1;
    case 2: outputFile << result << endl;
        return 1;
    case 3: outputFile << result << endl;
        return 1;
    case 4: outputFile << result << endl;
        return 1;
    default: cout << choice << " is not a valid input" << endl;
    }
    
    
    inputFile.close();
    outputFile.close();
    
    
    
    }
    

1 个答案:

答案 0 :(得分:0)

我相信下面虽然没有使用开关,但确实可以完成你所希望的,即找到最多4个匹配的结果,然后选择一个人。由于我没有文本文件,我不确定它是否正常工作,但它编译,似乎应该。我试图简化我可以的地方,同时保持原始意图(但我认为原始关闭的地方是该结果保留了最后一个读取匹配字符串,因此无法选择较早的那个。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main()
{
int choice;
int counter = 0;
string player, result;
string results[4];


cout << "\n\nThis program will ask the user for a player name from\n";
cout << "the team Dallas Mavericks and give the statistics of the player.\n";
cout << "  *Last Name First\n";
cout << "  *Seperated By Comma\n";
cout << "  *Use correct capitalization\n";
cout << "Please enter player's name: ";
getline(cin, player);

ifstream inputFile;
inputFile.open("statisticsMavs.txt");
if (inputFile.fail())
{
    cout << "Could not open file";
    return 1;
}

ofstream outputFile;
outputFile.open("playerStats.txt");

if (outputFile.fail())
{
    cout << "Could not open file";
    inputFile.close();
    return 1;
}

while ((counter<4) && getline(inputFile, result)) 
{
    if (result.find(player) != string::npos)
    {   
        results[counter++]=result;
        cout << endl << counter << "  " << result.substr(0, 20) << endl;

    }


}
if (counter == 0)
    {
        cout << "Could not find player! Terminating."<<endl;
    }
else
{
cout << "Please choose the number from the correct search result: ";
cin >> choice;

if ((choice>=1) && (choice<=counter))
{
    outputFile << results[choice-1] << endl;
    inputFile.close();
    outputFile.close();
    return 1;
}
else
{
cout << choice << " is not a valid input" << endl;
}
}

inputFile.close();
outputFile.close();



}