返回列表 回复 发帖
老师第200页本章实例中的头文件是不是还要加#include<iomanip>还有#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;
}
结构student后面应该加个分号。
我运行的时按了一下3,在添加数据成员一个,在按1,会出现很多的乱码,在还有如果在添加数据成员中加入成绩时,按进去的是字母也会出现很多的乱码。是不是我的编译器有问题
最上面一行的#include<string>是程序里的,不小心粘到上面去了
#include"stdafx.h"是干什么用的,我的这条会错
返回列表