| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 586 人关注过本帖
标题:回答关于" information of employee "输入输出问题(原帖找了很久找不到) ...
只看楼主 加入收藏
zzy840208
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2008-4-16
收藏
 问题点数:0 回复次数:1 
回答关于" 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;
}
搜索更多相关主题的帖子: employee information 输出 输入 
2008-06-04 23:00
zzy840208
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2008-4-16
收藏
得分:0 
我的对此问题的修改,也不知道正确与否,望大家指正,谢谢!
#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;
}
2008-06-04 23:02
快速回复:回答关于" information of employee "输入输出问题(原帖找了很久找不 ...
数据加载中...
 
   



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

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