| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 439 人关注过本帖
标题:帮忙看哈那里出了错啊!!
取消只看楼主 加入收藏
雪冬青
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-6-17
收藏
 问题点数:0 回复次数:0 
帮忙看哈那里出了错啊!!
//=========================================================
//     此程序是一个职工管理系统程序,主要实现的功能是
//     增加职工信息记录、显示职工信息、修改职工信息、
//     删除职工信息、统计职工信息.并且将职工信息以2进
//     制的形式写入文件中来保存.此程序是一个菜单程序.
//=========================================================

#include<iostream>
#include<string.h>
#define MAXNUM 30
using namespace std;

//选择的操作(1-6)
int choice=0;

//文件指针&文件名字
FILE *fp;
char f_name[]="employee.txt";

//职工结构
struct Employee{
    char   name[20];                 //姓名
    int    ID;                       //ID号
    char   sex[10];                  //性别
    int    age;                      //年龄
    double wage;                     //工资
};

//保存职工信息的数组
struct Employee Emp_item[MAXNUM];

//需要查找或者修改或者删除的职工姓名
char eg_name[20];

//保存在数组中职工的个数
int total_num=0;

//定义一个菜单函数
void main_menu(){
    cout<<"||================================||"<<"\n"
        <<"||          职工管理系统          ||"<<"\n"
        <<"||   1:输入职工信息               ||"<<"\n"
        <<"||   2:显示职工信息               ||"<<"\n"
        <<"||   3:修改职工信息               ||"<<"\n"
        <<"||   4:删除职工信息               ||"<<"\n"
        <<"||   5:统计职工信息               ||"<<"\n"
        <<"||   6:退出系统                   ||"<<"\n"
        <<"||================================||"<<endl;
    cout<<"请输入你需要的操作(1-6):";
    cin>>choice;
    if(choice>6 || choice<1){
        cout<<"操作失败!"<<endl;
        exit(1);
    }
}

//将文件中的信息读入数组Emp_item[]中
void readin(FILE *fp){
    int number=0;
    struct Employee temp;
    if((fp=fopen(f_name,"r"))==NULL){
        cout<<"文件无法打开或者出错";
        exit(1);
    }
    for(;!feof(fp);){
        fread(&temp,sizeof(Employee),1,fp);
        memcpy(&Emp_item[number++],&temp,sizeof(Employee));
    }
    total_num=number;
    fclose(fp);
}


//定义一个输入函数
void input_item(struct Employee item,FILE *){
     cout<<"请输入职工姓名:";
     cin>>item.name;
     cout<<"请输入职工ID号:";
     cin>>item.ID;
     cout<<"请输入职工性别:";
     cin>>item.sex;
     cout<<"请输入职工年龄:";
     cin>>item.age;
     cout<<"请输入职工工资:";
     cin>>item.wage;
     if((fp=fopen(f_name,"a+"))==0){
         cout<<"文件无法打开"<<endl;
         exit(1);
     }
     fwrite(&item,sizeof(Employee),1,fp);
     fclose(fp);
}

//定义一个输出函数
void output_item(struct Employee item){
    cout<<"姓名\tID号\t性别\t年龄\t工资\n"
        <<item.name<<"\t"<<item.ID<<"\t"<<item.sex<<"\t"<<item.age<<"\t"<<item.wage<<endl;
}

//主程序入口
int main(){
    main_menu();
    while(choice!=6){
        switch(choice){
        case 1:{
            struct Employee tmp;
            input_item(tmp,fp);
            main_menu();
               }
            break;
        case 2:{
            cout<<"输入职工姓名来显示此职工的所有信息:";
            cin>>eg_name;
            readin(fp);
            //找到匹配职工信息时显示
            for(int i=0;i<total_num-1;i++){
                if(strcmp(eg_name,Emp_item[i].name)==0)
                    output_item(Emp_item[i]);
            }
            //没有找到匹配职工信息时显示
            if((i==total_num-1) && strcmp(eg_name,Emp_item[i].name)!=0){
                cout<<"没有找到匹配的职工信息"<<endl;
                exit(1);
            }
            //for(int i=0;i<total_num-1;i++){
            //    output_item(Emp_item[i]);
            //}
            main_menu();
               }
            break;
        case 3:{
            struct Employee item_before;
            cout<<"输入职工姓名来修改此职工的信息:";
            cin>>eg_name;
            readin(fp);
            //找到匹配职工姓名时修改此职工的信息
            for(int i=0;i<total_num-1;i++){
                if(strcmp(eg_name,Emp_item[i].name)==0){
                    memcpy(&item_before,&Emp_item[i],sizeof(Employee));
                    cout<<"找到了匹配的职工姓名,请输入新的职工信息"<<endl;
                    cout<<"新的姓名:";
                    cin>>Emp_item[i].name;
                    cout<<"新的ID号:";
                    cin>>Emp_item[i].ID;
                    cout<<"新的性别:";
                    cin>>Emp_item[i].sex;
                    cout<<"新的年龄:";
                    cin>>Emp_item[i].age;
                    cout<<"新的工资:";
                    cin>>Emp_item[i].wage;
                    break;
                }
            }
            
            if((fp=fopen(f_name,"w"))==NULL){
                cout<<"文件无法打开或者出错"<<endl;
                exit(1);
            }
            for(int j=0;j<total_num-1;j++){
                fwrite(&Emp_item[j],sizeof(Employee),1,fp);
            }
            fclose(fp);
            
            cout<<"修改前的信息为:";
            output_item(item_before);
            cout<<"修改后的信息为:";

            output_item(Emp_item[i]);
            main_menu();
               }
            break;
        case 4:{
            cout<<"请输入你要删除的职工姓名:";
            cin>>eg_name;
            readin(fp);
            for(int i=0;i<total_num-1;i++){
                if(strcmp(eg_name,Emp_item[i].name)==0){
                    for(int j=0;j<total_num-i-1;j++){
                        memcpy(&Emp_item[i],&Emp_item[i+1],sizeof(Employee));
                        i++;
                    }
                    break;
                }
            }
            if((fp=fopen(f_name,"w"))==NULL){
                cout<<"文件无法打开或者出错"<<endl;
                exit(1);
            }
            for(int k=0;k<total_num-2;k++){
                fwrite(&Emp_item[k],sizeof(Employee),1,fp);
            }
            fclose(fp);
            main_menu();
               }
            break;
        case 5:{
            //定义平均工资和总工资
            double avg_wage,total_wage=0;
            readin(fp);
            for(int i=0;i<total_num-1;i++){
                total_wage+=Emp_item[i].wage;
            }
            avg_wage=total_wage/total_num;
            cout<<"平均工资为:"<<avg_wage<<"\n"
                <<"总工资为:"<<total_wage<<endl;
            main_menu();
               }
            break;
        case 6:
            break;
        default:
            break;
        }
    }
    return 0;
}
搜索更多相关主题的帖子: 信息 管理系统 include choice 
2008-06-24 10:53
快速回复:帮忙看哈那里出了错啊!!
数据加载中...
 
   



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

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