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(); }
如果他们选择了球员,你找到了球员,他们再次选择,因为你可能在同一球队中有两个史密斯球员? –
哦,我看到了问题,我敢打赌,如果你只匹配1,它就可以正常工作,但如果有多个比赛,结果 –
专门将结果移动到一个新行并将其声明为字符串结果[4]。使getline(inputFile,result)getline(inputFile,result [counter-1] )用[counter-1]索引.find,用counter-2索引索引,并且在每种情况下(只要他们说1 t o 4你甚至不需要开关)放出结果[counter-1] –