![]() |
#2
zzy8402082008-06-04 23:02
我的对此问题的修改,也不知道正确与否,望大家指正,谢谢!
#include <iostream>
#include <fstream> #include <sstream> #include <string> using namespace std; struct employee { int num; string name; int age; float wage; }; ifstream ifile("employee.txt"); ofstream ofile("employee.txt",ios::out|ios::app); void input() { employee temp; cout <<"Input information of employee:" <<endl; cout <<"number:"; cin>>temp.num; cout <<"name:"; cin>>temp.name; cout <<"age:"; cin>>temp.age; cout <<"wage:"; cin>>temp.wage; cout <<endl; ofile <<temp.num <<" " <<temp.name <<" " <<temp.age <<" " <<temp.wage <<endl; } void outputAll() { employee temp; string line; while(getline(ifile,line)) { istringstream is(line); is>>temp.num>>temp.name>>temp.age>>temp.wage; cout <<"number:" <<temp.num <<" "; cout <<"name:" <<temp.name <<" "; cout <<"age:" <<temp.age <<" "; cout <<"wage:" <<temp.wage <<endl; } } void check() { cout<<"Input the employee's number that you want to check:" <<endl; int num; cin>>num; employee temp; string line; while(getline(ifile,line)) { istringstream is(line); int temp_num; is>>temp_num; if(temp_num==num) { temp.num=temp_num; is>>temp.name>>temp.age>>temp.wage; goto quit; } } cout<<"sorry! No." <<num <<" do not exist." <<endl; return; quit: cout<<temp.num<<" "<<temp.name<<" "<<temp.age<<" "<<temp.wage<<endl; } int main() { if(!ifile.is_open()) { cout<< "Could not open the file \n"<< endl; exit(1); } cout <<"welcome to the management system of information of employee" <<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 information" <<endl; cout <<"press 0 quit" <<endl; int order; do { cout <<"order:"; cin>>order; switch(order) { case 1: input(); break; case 2: check(); break; case 3: outputAll(); break; case 0: goto quit; break; default: cerr <<"error!" <<endl; } }while(order!=0); quit: ifile.close(); ofile.close(); system("pause"); return 0; } |
原问题代码:
#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;
}