#include<string>
#include<iomanip>
#include<fstream>
#include<iostream>
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 finddata()
{
string sname;
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(student)))
{
if(mystudent.name==sname)
{
iffind=true;
cout<<mystudent.name<<mystudent.course<<mystudent.score<<endl;
}
}
if(!iffind)
{cout<<"没有该学生"<<endl;
}file.close();
}
void adddata()
{
fstream file("student.dat",ios::out|ios::app);
cout<<"添加数据";
student mystudent;
cin>>mystudent.name>>mystudent.course>>mystudent.score;
file.write((char*)&mystudent,sizeof(student));
file.close();
}
int main()
{
int select;
do
{
cout<<"选择:1输出全部学生成绩"<<endl;
cout<<setw(22)<<"2按姓名查询成绩"<<endl;
cout<<setw(22)<<"3添加新成绩"<<endl;
cin>>select;
switch(select)
{
case 1:dispdata();break;
case 2:finddata();break;
case 3:adddata();break;
default:break;
}
}while(select==1||select==2||select==3);
return 0;
}这个程序帮我运行下,是不是我的编译器有问题,我加了个成员进去,在按1,结果,好多都是乱码 |