返回列表 发帖

本书第200页本章实例(I/O章)

#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,结果,好多都是乱码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

你最好能发一个图。没有图很难判断的。

TOP

这里怎么把截图粘上去的

TOP

3# twtyqqqf


你把图片传到tu.6.cn,然后就能得到一个图片连接。然后在帖子中插入图片,就OK了。

tu.6.cn是一个免费的图床。

TOP

4# fwbook




TOP

5# twtyqqqf

TOP

老师这样好像看不清楚

TOP

你抓的图也太小了。抓几张大图吧。

TOP

TOP

http://tu.6.cn/pic/show-new/id/10544645/这一张好像进入死循环

TOP

看看循环语句是不是有错误

TOP

返回列表