2016-12-04 60 views
1
  1. 该代码要求用户从团队中挑选一名玩家,并显示该玩家的统计数据。搜索必须能够进行部分输入搜索。这是我迄今为止提出的代码。但是我被卡在交换机工作正常或搜索功能之间,但不能使它们一起工作。谢谢您的帮助。在C++中,如何结合搜索功能和开关,所以开关可以要求用户从搜索结果中选择并显示结果信息?

    #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(); 
    
    
    
    } 
    
+0

如果他们选择了球员,你找到了球员,他们再次选择,因为你可能在同一球队中有两个史密斯球员? –

+0

哦,我看到了问题,我敢打赌,如果你只匹配1,它就可以正常工作,但如果有多个比赛,结果 –

+0

专门将结果移动到一个新行并将其声明为字符串结果[4]。使getline(inputFile,result)getline(inputFile,result [counter-1] )用[counter-1]索引.find,用counter-2索引索引,并且在每种情况下(只要他们说1 t o 4你甚至不需要开关)放出结果[counter-1] –

回答

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(); 



}