// 从文件中加载参赛队伍信息 void loadTeamsFromFile(vector<Team>& teams) { ifstream inFile("team.txt"); if (!inFile.is_open()) { cout << "无法打开team.txt文件!" << endl; return; } string line; while (getline(inFile, line)) { stringstream ss(line); Team team; if (ss >> team.id) { ss >> std::ws; getline(ss, team.name, '\t'); ss >> std::ws; getline(ss, team.school, '\t'); ss >> std::ws; getline(ss, team.category, '\t'); ss >> std::ws; getline(ss, team.players, '\t'); ss >> std::ws; getline(ss, team.coach); cout << "id: " << team.id << ", name: " << team.name << ", school: " << team.school << ", category: " << team.category << ", players: " << team.players << ", coach: " << team.coach << endl; teams.push_back(team); } else { cout << "无效行:" << line << endl; } } }// 按学校查询参赛队伍信息 void searchBySchool(vector<Team>& teams) { string school; cout << "请输入要查询的学校名称:" << endl; cin >> school; bool found = false; for (auto& team : teams) { if (team.school == school) { cout << "编号:" << team.id << endl; cout << "名称:" << team.name << endl; cout << "学校:" << team.school << endl; cout << "类别:" << team.category << endl; cout << "参赛者:" << team.players << endl; cout << "教练姓名:" << team.coach << endl; found = true; } } if (!found) { cout << "无法找到该学校参赛队伍,请检查输入是否正确!" << endl; } }在运行输入的学校无误后显示未查找到该学校的参赛队伍
时间: 2023-10-30 22:15:05 浏览: 136
可能是因为加载参赛队伍信息时,有些行的学校名称与你输入的学校名称不完全一致,导致无法找到对应的参赛队伍。
你可以尝试修改 `searchBySchool` 函数,使用字符串的 `find` 函数来判断参赛队伍的学校名称中是否包含输入的字符串,从而实现模糊匹配。具体代码如下:
``` c++
// 按学校查询参赛队伍信息
void searchBySchool(const vector<Team>& teams) {
string school;
cout << "请输入要查询的学校名称:" << endl;
cin >> school;
bool found = false;
for (const auto& team : teams) {
if (team.school.find(school) != string::npos) {
cout << "编号:" << team.id << endl;
cout << "名称:" << team.name << endl;
cout << "学校:" << team.school << endl;
cout << "类别:" << team.category << endl;
cout << "参赛者:" << team.players << endl;
cout << "教练姓名:" << team.coach << endl;
found = true;
}
}
if (!found) {
cout << "无法找到包含 " << school << " 的参赛队伍,请检查输入是否正确!" << endl;
}
}
```
这样就可以实现模糊匹配,查找到包含输入的字符串的参赛队伍了。
阅读全文