本帖最后由 zx4219770 于 2011-4-16 21:15 编辑
1.把那些预编译的头文本的.h都去掉,这个是VC++6.0编译器的写法
2.void fidata()函数的后面少了一个括号“}”
3.第57行的 cin>>mystudent.couse; 代码写错了,应该是cin>>mystudent.course;
下面是修改后的代码:- #include<string>
- #include<iostream>
- #include<fstream>
- #include<iomanip>
- using namespace std;
- struct student
- {
- string name;
- string course;
- int score;
- };
- void dispdata()
- {
- ifstream file("student.dat");
- student my_student;
- cout<<"输出全部学生成绩:"<<endl;
- cout<<setw(12)<<"姓名"<<setw(8)<<"课程"<<setw(12)<<"成绩"<<endl;
- while(file.read((char*)&my_student,sizeof(student)))//非常重要
- {
- cout<<setw(12)<<my_student.name
- <<setw(8)<<my_student.course
- <<setw(12)<<my_student.score<<endl;
- }
- file.close();//关闭文件;
- }
- void fidata()
- {
- char sname[6];
- bool iffind=false;
- ifstream file("student.dat");
- student mystudent;
- file.seekg(0);
- cout<<"输入要查询的学生的姓名:"<<endl;
- cin>>sname;
- cout<<setw(12)<<"姓名"<<setw(8)<<"课程"<<setw(12)<<"分数"<<endl;
- while(file.read((char*) &mystudent,sizeof(mystudent)))
- {
- if(mystudent.name==sname)
- {
- iffind=true;
- cout<<setw(12)<<mystudent.name
- <<setw(8)<<mystudent.course
- <<setw(12)<<mystudent.score<<endl;
- }
- if(!iffind)
- cout<<"没有这个学生!";
- file.close();
- }
- }
- int addata()
- {
- student mystudent;
- fstream file("student.dat",ios::out|ios::app);
- cout<<"添加数据(姓名 课程 分数):";
- cin>>mystudent.name;
- cin>>mystudent.course;
- cin>>mystudent.score;
- file.write((char*) &mystudent,sizeof(student));
- file.close();
- return 0;
- }
- int main()
- {
- int select;
- cin>>select;
- do
- {
- cout<<"choose 1:输出全部学生成绩"<<endl;
- cout<<" 2:按姓名查询成绩"<<endl;
- cout<<" 3:添加新成绩"<<endl;
- switch(select)
- {
- case 1:dispdata();break;
- case 2:fidata();break;
- case 3:addata();break;
- default:break;
- }
- }while(select==1||select==2||select==3);
- return 0;
- }
复制代码 |