逻辑错误
#include<iostream>#include<fstream>
#include<string>
using namespace std;
class base
{
public:
protected:string name;char sex;string addr;
};
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;
cin>>name;
cout<<"sex:"<<sex<<endl;
cin>>addr;
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();
}
}
}
}
}
刚学,不知道怎么改