帮忙看一下问题出在哪里
简易客户管理程序。1) 采用类来实现对客户(User)信息的描述;
2) 通过<登记>菜单选项实现客户信息(编号、姓名、性别、出生年月、身份证号、部门、职位、住宅电话、E-Mail、、通讯地址、个人说明)的登记(Add)工作;
3) 选择<查询>菜单选项时,按照指定用户名或身份证号显示该客户的各项信息(Search)。
4)把客户(User)信息存储在文件中。
请高手帮忙看看问题出在哪,感激不尽
Main.cpp
#include "user.h"
void main()
{
User *top;
ASK(top);
top->handle_menu(top);
}
User.cpp
#include"user.h"
int User::count=0; //声明静态记录计数变量
User::User()
{
next=NULL;
us.number[0]='0';
us.name[0]='0';
us.sex[0]='0';
us.birth[0]='0';
us.ID[0]='0';
us.branch[0]='0';
us.position[0]='0';
us.Tel[0]='0';
us.email[0]='0';
us.address[0]='0';
us.information[0]='0';
}
User::~User()
{
if(next!=NULL)
delete next;
}
//文件存储操作函数
void User::save(User *top)
{
if(top->us.name[0]=='0')
{
cout<<"\t没有记录可存!"<<endl;
return;
}
ofstream out;
out.open("user.txt",ios::out);
cout<<"\n\t存文件"<<endl;
User *p=top;
while(p!=NULL)
{
out<<*p<<endl;
p=p->next;endl;
}
out.close();
cout<<top->count<<"条记录已经存入文件,请继续操作。"<<endl;
}
//重载>>运算符函数
istream & operator>>(istream &is,User &ob)
{
is.getline(ob.us.number,8,'\n');
is.getline(ob.us.name,16,'\n');
is.getline(ob.us.sex,4,'\n');
is.getline(ob.us.birth,10,'\n');
is.getline(ob.us.ID,12,'\n');
is.getline(ob.us.branch,10,'\n');
is.getline(ob.us.position,10,'\n');
is.getline(ob.us.Tel,12,'\n');
is.getline(ob.us.email,10,'\n');
is.getline(ob.us.address,100,'\n');
is.getline(ob.us.information,100,'\n');endl;
return is;
}
//重载<<运算符函数
ostream&operator<<(ostream&os,User&ob)
{
os<<ob.us.number<<'\n';
os<<ob.us.name<<'\n';
os<<ob.us.sex<<'\n';
os<<ob.us.birth<<'\n';
os<<ob.us.ID<<'\n';
os<<ob.us.branch<<'\n';
os<<ob.us.position<<'\n';
os<<ob.us.Tel<<'\n';
os<<ob.us.email<<'\n';
os<<ob.us.address<<'\n';
os<<ob.us.information<<'\n'<<endl;;
return os;
}
//菜单选择函数
int User::menu_select()
{
char u[2];
int cn=0;
cout<< '\n'
<<"\t1.登记\n"
<<"\t2.保存文件\n"
<<"\t3.查询\n"
<<"\t4.结束程序运行\n"
<<"\n\t 请选择1--4:"<<flush;
for(;;)
{
gets(u);
cn=atoi(u);
if(cn<1||cn>5) cout<<"\n\t输入错误,重选1--4:"<<flush;
else break;
}
return cn;
}
//输入信息函数
User *User::input(User *top)
{
ifstream in;
in.open("user.txt",ios::out);
User *old,*star;
ASK(star);
old=top;
while(old->next!=NULL)
{
old=old->next;
}
cout<<"\t输入数据,输入0时结束。"<<endl;
--top->count;
do
{
++top->count;
cout<<"\t编号:"<<flush;
gets(star->us.number);
if(strcmp(star->us.number,"0")==0)
break;
cout<<"\t姓名:"<<flush;
gets(star->us.name);
cout<<"\t性别:"<<flush;
gets(star->us.sex);
cout<<"\t出生年月:"<<flush;
gets(star->us.birth);
cout<<"\t身份证号:"<<flush;
gets(star->us.ID);
cout<<"\t部门:"<<flush;
gets(star->us.branch);
cout<<"\t职位:"<<flush;
gets(star->us.position);
cout<<"\t住宅电话:"<<flush;
gets(star->us.Tel);
cout<<"\temail:"<<flush;
gets(star->us.email);
cout<<"\t住址:"<<flush;
gets(star->us.address);
cout<<"\t个人说明:"<<flush;
gets(star->us.information);endl;
if(top->count==0) top=star;
old->next=star;
old=star;
ASK(star);
}
while(1);
old->next=NULL;
return(top);
in.close();
}
//查询信息函数
void User::find_record(User *top)
{
char choose[2],input[30];
int cn=0;
cout<<"\t1.通过用户名查询"<<endl;
cout<<"\t2.通过身份证号查询"<<endl;
cout<<"\t请选择1--2:"<<flush;
gets(choose);
while(1)
{
cn=atoi(choose);
if(cn!=1&&cn!=2)
{
cout<<"\t输入错误,重选1--2:"<<flush;
gets(choose);
}
else break;
}
switch(cn)
{
case 1:
cout<<"\t请输入姓名:"<<flush;
break;
case 2:
cout<<"\t请输入身份证号:"<<flush;
break;
}
gets(input);
User *p;
p=top;
ifstream file;
file.open("user.txt",ios::in);
file>>*p; //读文件
while(p!=NULL)
{
if((cn==1&& strcmp(input,p->us.name))==0||(cn==2&& strcmp(input,p->us.ID)==0))
{
cout<<"\t找到信息如下:"<<endl;
cout<<"\t编号\t姓名\t性别\t出生年月\t身份证号\n"<<endl;
cout<<'\t'<<p->us.number<<'\t'<<p->us.name<<'\t'<<p->us.sex<<'\t'<<p->us.birth<<'\t'<<p->us.ID<<'\n'<<endl;
cout<<"\t部门\t职位\t住宅电话\temail\t住址\t个人说明\n"<<endl;
cout<<'\t'<<p->us.branch<<'\t'<<p->us.position<<'\t'<<p->us.Tel<<'\t'<<p->us.email<<'\t'<<p->us.address<<'\t'<<p->us.information<<'\n'<<endl;
return;
}
else
{
p=p->next;
}
}
cout<<"\t没有找到相应的记录。"<<endl;
file.close;
return;
}
//菜单处理函数
void User::handle_menu(User *top)
{
for(; ;){
switch(menu_select())
{
case 1:
top=input(top);
break;
case 2:
save(top);
break;
case 3:
find_record(top);
break;
case 4:
cout<<"\t退出操作"<<endl;
delete top;
return;
}
}
}
User.h
#if !defined(USER_H)
#define USER_H
#include<fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include<string.h>
#include<iostream.h>
#define ASK(p) do{ \
p=new User; \
if(p==NULL) {cout<<"memory feil!"<<endl;exit(-1);} \
}while(0)
//客户信息结构
struct use
{
char number[8]; //编号
char name[16]; //姓名
char sex[4]; //性别
char birth[10]; //出生年月
char ID[12]; //身份证号
char branch[10]; //部门
char position[10]; // 职位
char Tel[12]; //住宅电话
char email[10]; //email
char address[100]; //住址
char information[100]; //个人说明
};
//客户类
class User
{
private:
use us;
User *next;
int menu_select(void);
public:
static int count;
User();
~User();
User *slstore(User *);
void save(User *);
User *input(User *);
void handle_menu(User *);
void find_record(User *);
User *load(User *);
friend ostream &operator<<(ostream &os,User &ob);
friend istream &operator>>(istream &is,User &ob);
};
#endif