回答关于" information of employee "输入输出问题(原帖找了很久找不到)
原问题代码:#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const unsigned int N=7;//职员个数
struct employee//定义employee结构体
{
int num;
string name;
int age;
float wage;
};
employee emp[N];
int i;
int order=4;
ifstream ifile;//定义输入文件流ifile
ofstream ofile;//定义输出文件流ofile
void input()
{
employee temple;
//ofstream ofile;
cout <<"input information of employee:" <<endl;//输入T个职员的资料
cout <<"number:";
cin>>temple.num;
cout <<"name:";
cin>>temple.name;
cout <<"age:";
cin>>temple.age;
cout <<"wage:";
cin>>temple.wage;
cout <<endl;
ofile <<temple.num <<" " <<temple.name <<" " <<temple.age <<" " <<temple.wage <<endl;
}
void outputall(struct employee *e)
{
//ifstream ifile;
for(i=0;i <N;i++)//输出全部职员的资料
{
ifile>>e->num;
cout <<"number:" <<e->num <<" ";
ifile>>e->name;
cout <<"name:" <<e->name <<" ";
ifile>>e->age;
cout <<"age:" <<e->age <<" ";
ifile>>e->wage;
cout <<"wage:" <<e->wage <<endl;
e++;
}
cout <<endl;
}
void check(struct employee *e)
{
int num;
//ifstream ifile;
cout <<"input the employee's number:" <<endl;//输入职员号码(num)输出职员资料
cin>>num;
for(i=0;i <N;i++)
{
if(e->num==num)
{
ifile>>e->num;
cout <<"number:" <<e->num <<" ";
ifile>>e->name;
cout <<"name:" <<e->name <<" ";
ifile>>e->age;
cout <<"age:" <<e->age <<" ";
ifile>>e->wage;
cout <<"wage:" <<e->wage <<endl;
goto out;
}
e++;
}
cerr <<"sorry! No." <<num <<" do not exist." <<endl;
out: ;
}
int main()
{
//ifile.open("employee.txt",ios::in);
//ifstream ifile;
//ofstream ofile;
ifile.open("employee.txt");
if(!ifile.is_open())
{
//cerr <<"open employee.txt error!" <<endl;
cout<< "Could not open the file \n"<< endl;
exit(1);
}
ofile.open("employee.txt",ios::out|ios::app);
cout <<"welcome to the information of employee management system" <<endl;
cout <<"press 1 input information of employee" <<endl;
cout <<"press 2 look up the employee through the number" <<endl;
cout <<"press 3 output all tinformation" <<endl;
cout <<"press 0 quit" <<endl;
while(order!=0)
{
cout <<"order:";
cin>>order;
switch(order)
{
case 1: input(); break;
case 2: check(emp); break;
case 3: outputall(emp); break;
case 0: goto quit; break;
default: cerr <<"error!" <<endl;
}
}
quit:
ifile.close();
ofile.close();
system("pause");
return 0;
}