写了一个代码,如下,student结构体的定义我也贴在下面了:
typedef struct _student
{
string jiaoshougognhao;
string xingming;
string zhicheng;
string xueyuan;
string zubie;
}student;
void studentchaxun()
{
int flag = 0;
string out;
string xueyuan;
student s;
cout << "请输入学院:";
cin >> xueyuan;
ifstream ifs("选手报名信息.txt", ios::in);
if (!ifs.is_open())
{
cout << "不存在该文件";
//ifs.close(); //这一句不需要,因为文件不存在,ifs是0
}
else
{
while (ifs >> out >> s.jiaoshougognhao >> s.xingming >> s.zhicheng >> s.xueyuan >> s.zubie)
{
if (s.xueyuan.compare(xueyuan) == 0)
{
cout << s.jiaoshougognhao << " " << s.xingming << " " << s.zhicheng << " " << s.xueyuan << " " << s.zubie << endl;
flag = 1;
}
}
ifs.close();
if (flag == 0)
{
cout << "错误" << endl;
cout << "未命名" << endl;
}
}
}
Xueyuan不应该是char类型吧,如果输入的不是单个字符,就匹配不上了。
对所有学生进行遍历,如果当前学生是查询学院的再输出他的信息
你这函数根本没调用呀
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!