| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1559 人关注过本帖
标题:刚学,不知道怎么改
只看楼主 加入收藏
你爸边哭边
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2017-10-30
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:3 
刚学,不知道怎么改
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class base
{
public:
protected:char name[20];char sex;char addr[20];
};
class teacher:public base
{public:
void show1();
void display1();
private:
    float wages;
};
class student:public base
{
public:
    void show2();
    void display2();
private:
    float grade;
};
void teacher::display1()
{
    cout<<"请输入姓名:"<<endl;
   
    cout<<"请输入性别"<<endl;
    cin>>sex;
    cout<<"请输入地址"<<endl;

    cout<<"请输入工资"<<endl;
    cin>>wages;
}
void student::display2()
{
    cout<<"请输入姓名:"<<endl;
    cin>>name;
    cout<<"请输入性别"<<endl;
    cin>>sex;
    cout<<"请输入地址"<<endl;
    cin>>addr;
    cout<<"请输入成绩"<<endl;
    cin>>grade;
}
void teacher::show1()
{    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"address:"<<addr<<endl;
    cout<<"wages:"<<wages<<endl;
}
void student::show2()
{    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"address:"<<addr<<endl;
    cout<<"grade:"<<grade<<endl;
}
#include<fstream>
void main()
{
    int a,b,i,c;
    cout<<"请选择:1.输入  2.查询"<<endl;
    cin>>a;
    if(a==1)
    {
        cout<<"请选择: 1老师 2学生"<<endl;
        cin>>b;
        if(b==1)
        {
            teacher tea[2];
            do
            {
                tea[2].display1();
                i--;
            }while(i==0);
            ofstream outfile("teacher.dat",ios::binary|ios::trunc);
            if(!outfile)
            {cerr<<"open error"<<endl;
            abort();
            }
            for(i=0;i<2;i++)
                outfile.write((char*)&tea[i],sizeof(tea[i]));
            outfile.close();
            
        }
        if(b==2)
        {   
            student stud[2];
            do
            {
                stud[2].display2();
                i--;
            }while(i==0);
            ofstream outfile("student.dat",ios::binary|ios::trunc);
            if(!outfile)
            {cerr<<"open error"<<endl;
            abort();
            }
            for(i=0;i<2;i++)
                outfile.write((char*)&stud[i],sizeof(stud[i]));
            outfile.close();
        }
        
    }
    if(a==2)
    {
        cout<<"请选择: 1老师 2学生"<<endl;
        cin>>c;
        if(c==1)
        {   
            teacher tea[2];
            ifstream infile("teacher.dat",ios::binary);
            if(!infile)
            {cerr<<"open error"<<endl;
            abort();
            }
            for(i=0;i<2;i++)
                infile.read((char*)&tea[i],sizeof(tea[i]));
            infile.close();
            for(i=0;i<2;i++)
            {
                tea[i].show1();
            }
            if(c==2)
            {   
                student stud[2];
                ifstream infile("student.dat",ios::binary);
                if(!infile)
                {cerr<<"open error"<<endl;
                abort();
                }
                for(i=0;i<2;i++)
                    infile.read((char*)&stud[i],sizeof(stud[i]));
                infile.close;
                for(i=0;i<2;i++)
                {
                    stud[i].show2();
                }
               
            }
        }
    }
}
刚学,不知道怎么改
搜索更多相关主题的帖子: void student cout 输入 cin 
2018-05-29 21:13
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:14 
只負責看編譯錯誤, 具體的運行問題可以自行 debug

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class base
{
public:
protected:char name[20];char sex;char addr[20];
};
class teacher:public base
{public:
void show1();
void display1();
private:
    float wages;
};
class student:public base
{
public:
    void show2();
    void display2();
private:
    float grade;
};
void teacher::display1()
{
    cout<<"请输入姓名:"<<endl;
   
    cout<<"请输入性别"<<endl;
    cin>>sex;
    cout<<"请输入地址"<<endl;

    cout<<"请输入工资"<<endl;
    cin>>wages;
}
void student::display2()
{
    cout<<"请输入姓名:"<<endl;
    cin>>name;
    cout<<"请输入性别"<<endl;
    cin>>sex;
    cout<<"请输入地址"<<endl;
    cin>>addr;
    cout<<"请输入成绩"<<endl;
    cin>>grade;
}
void teacher::show1()
{    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"address:"<<addr<<endl;
    cout<<"wages:"<<wages<<endl;
}
void student::show2()
{    cout<<"name:"<<name<<endl;
    cout<<"sex:"<<sex<<endl;
    cout<<"address:"<<addr<<endl;
    cout<<"grade:"<<grade<<endl;
}
#include<fstream>
int main()
{
    int a,b,i,c;
    cout<<"请选择:1.输入  2.查询"<<endl;
    cin>>a;
    if(a==1)
    {
        cout<<"请选择: 1老师 2学生"<<endl;
        cin>>b;
        if(b==1)
        {
            teacher tea[2];
            do
            {
                tea[2].display1();
                i--;
            }while(i==0);
            ofstream outfile("teacher.dat",ios::binary|ios::trunc);
            if(!outfile)
            {cerr<<"open error"<<endl;
            abort();
            }
            for(i=0;i<2;i++)
                outfile.write((char*)&tea[i],sizeof(tea[i]));
            outfile.close();
            
        }
        if(b==2)
        {   
            student stud[2];
            do
            {
                stud[2].display2();
                i--;
            }while(i==0);
            ofstream outfile("student.dat",ios::binary|ios::trunc);
            if(!outfile)
            {cerr<<"open error"<<endl;
            abort();
            }
            for(i=0;i<2;i++)
                outfile.write((char*)&stud[i],sizeof(stud[i]));
            outfile.close();
        }
        
    }
    if(a==2)
    {
        cout<<"请选择: 1老师 2学生"<<endl;
        cin>>c;
        if(c==1)
        {   
            teacher tea[2];
            ifstream infile("teacher.dat",ios::binary);
            if(!infile)
            {cerr<<"open error"<<endl;
            abort();
            }
            for(i=0;i<2;i++)
                infile.read((char*)&tea[i],sizeof(tea[i]));
            infile.close();
            for(i=0;i<2;i++)
            {
                tea[i].show1();
            }
            if(c==2)
            {   
                student stud[2];
                ifstream infile("student.dat",ios::binary);
                if(!infile)
                {cerr<<"open error"<<endl;
                abort();
                }
                for(i=0;i<2;i++)
                    infile.read((char*)&stud[i],sizeof(stud[i]));
                infile.close();
                for(i=0;i<2;i++)
                {
                    stud[i].show2();
                }
               
            }
        }
    }
}
2018-05-29 22:04
你爸边哭边
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2017-10-30
收藏
得分:0 
回复 2楼 Jonny0201
语法没问题,好像是逻辑错误
2018-05-30 10:46
快速回复:刚学,不知道怎么改
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017899 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved