问题描述
- C加加有错,不知错在哪。求解?? 234不能用?
-
#include
#include
#include
using namespace std;
void print(); //声明实现第一功能的函数
//void shanchu();//void add();
//void again();
//void iofile();
//void show_menu();class putong //定义一个普通账户的类
{
public:
putong(int n,string nam,double m)
{
num=n;
name=nam;
money=m;} //int get_num(); //string get_name(); //double get_money(); //void set_num(); //void set_name(); //void set_money(); int num; //账号 string name; //姓名 double money; //余额
};
class vip:public putong //定义一个VIP,继承putong类
{
public:
vip(int n,string nam,double m,int s):putong(n,nam,m)
{
score=s;
}
//int get_score();int score; //积分
};/*int putong::get_num()
{return num;
}string putong::get_name()
{return name;
}double putong::get_money()
{return money;
}
int vip::get_score(){
return score;
}/*void set_num()
{
cin>>num;
}void set_name()
{
cin>>name;
}
void set_money()
{
cin>>money;}*/
void show_menu() //开始菜单
{cout<<''<<''<<''<<"银行储户信息管理系统"<<endl; cout<<"----------------------------------"<<endl; cout<<"1.打印客户信息"<<endl; cout<<"2.更新客户信息"<<endl; cout<<"3.添加客户信息"<<endl; cout<<"4.删除客户信息"<<endl; cout<<"5.退出程序"<<endl; cout<<"----------------------------------"<<endl;
}
int main()
{putong a[5]= {putong(1,"刘敏",-3500), //初始化
putong(2,"w",1200.7),
putong(3,"吴丽", -220),
putong(4,"王世明",10000),
putong(5,"张明华",300.32)};int key; show_menu(); //显示开始菜单 //vip b(); void iofile(); while (1) { cout<<'n'<<"请选择功能<1-5>:"; cin>>key; switch (key) { case 1: print(); continue; case 2: {int n=5; putong b(0,"0",0); //建立一个临时变量 int A; string B; double C; fstream iofile; iofile.open("Client.dat",ios::out|ios::binary); iofile.seekg(0,ios::beg); for(int f=0;f<5;f++) { iofile.read((char*)&a[f],sizeof(a[f])); } cout<<"请输入账号"<<''<<''<<"姓名"<<''<<''<<"余额"<<endl; cin>>A>>B>>C; putong e(A,B,C); iofile.seekg((A-1)*sizeof(b),ios::beg); iofile.write((char*)&e,sizeof(e)); iofile.seekg(0,ios::beg); iofile.read((char*)&e,sizeof(e)); for( f=0;f<5;f++) { //cout<<e.money <<e.name <<e.num <<endl; iofile.read((char*)&e,sizeof(e)); } cout<<"一条记录被更新!"<<endl; iofile.close(); continue; } case 3: { int add_num; string add_name; double add_value; //putong d(0,"0",0); //建立一个临时变量 fstream iofile; iofile.open("Client.dat",ios::out|ios::binary); iofile.seekg(0,ios::beg); for( int f=0;f<5;f++) { iofile.read((char*)&a[f],sizeof(a[f])); } cout<<"请输入账号"<<''<<''<<"姓名"<<''<<''<<"余额"<<endl; cin>>add_num>>add_name>>add_value; putong g(add_num,add_name,add_value); iofile.seekg((add_num-1)*sizeof(a[0]),ios::beg); iofile.write((char*)&g,sizeof(g)); iofile.seekg(0,ios::beg); iofile.read((char*)&g,sizeof(g)); for( f=0;f<5;f++) { iofile.read((char*)&g,sizeof(g)); } cout<<"已追加一条新记录!"<<endl; iofile.close(); continue; } case 4: { putong c(0," ",0); //建立一个临时变量 int n=5; int b; fstream iofile; iofile.open("Client.dat",ios::out|ios::binary); iofile.seekg(0,ios::beg); for(int i=0;i<n;i++) { iofile.read((char*)&a[i],sizeof(a[i])); } cout<<"请输入待删除的账号"<<endl; cin>>b; iofile.seekg((b-1)*sizeof(c),ios::beg); iofile.write((char*)&c,sizeof(c)); iofile.seekg(0,ios::beg); for(i=0;i<n;i++) { iofile.read((char*)&c,sizeof(c)); } cout<<"已删除!"<<endl; iofile.close(); continue; } case 5: exit(1) ; continue; default: cout<<"此输入无效!"<<endl; continue; } } return 0;
}
void iofile() //创建一个Client.dat文件,并读入数据
{
putong a(0,"0",0); //建立一个临时变量
fstream iofile("Client.dat",ios::out|ios::binary);
if(!iofile)
{
cerr<<"open error!"<<endl;
abort();
}for(int i=0;i<5;i++) //将数据读入文件里 { iofile.write((char*)&a,sizeof(a)); } iofile.close();
}
void print()
{fstream iofile;
iofile.open("Client.dat",ios::out|ios::binary); //打开Client.dat文件
putong a[5]= {putong(1,"刘敏",-3500), //初始化
putong(2,"w",1200.7),
putong(3,"吴丽", -220),
putong(4,"王世明",10000),
putong(5,"张明华",300.32)};
for(int i=0;i<5;i++) //读数据
{iofile.read((char*)&a[i],sizeof(a[i]));
cout<<"----------------------------------"<<endl;
if (a[i].money<100000) //如果读出的数据的余额小于1000000,则运行
{cout<<"普通账户信息:"<<endl; cout<<"账号"<<''<<''<<"姓名"<<''<<''<<"余额"<<endl; cout<<a[i].num<<''<<''<<a[i].name<<''<<''<<a[i].money<<endl; } else //否则则运行下面这个 { iofile.seekg(i*sizeof(a[i]),ios::beg); cout<<"VIP账户信息:"<<endl; cout<<"账号"<<''<<''<<"姓名"<<''<<''<<"余额"<<"VIP账户积分"<<endl; cout<<a[i].num<<''<<''<<a[i].name<<''<<''<<a[i].money<<''<<''<<a[i].money/10<<endl; };
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
fstream printfile("accouts.doc",ios::out); //创建一个accouts.doc文件 if(!printfile) { cout<<"open error!"<<endl; }; for(int i=0;i<5;i++) //读数据 { iofile.seekg(0,ios::beg); iofile.read((char*)&a,sizeof(a)); printfile<<"----------------------------------"<<endl; if (a[i].money<100000) //如果读出的数据的余额小于1000000 { printfile<<"普通账户信息:"<<endl; printfile<<"账号"<<''<<''<<"姓名"<<''<<''<<"余额"<<endl; printfile<<a[i].num<<''<<''<<a[i].name<<''<<''<<a[i].money<<endl; } else //如果读出的数据的余额大于或等于100000 { //iofile.seekg(i*sizeof(a[i]),ios::beg); printfile<<"VIP账户信息:"<<endl; printfile<<"账号"<<''<<''<<"姓名"<<''<<''<<"余额"<<"VIP账户积分"<<endl; printfile<<a[i].num<<''<<''<<a[i].name<<''<<''<<a[i].money<<''<<''<<a[i].money/10<<endl; } } printfile.close(); iofile.close(); cout<<"客户信息已输出至可打印文件中!"<<endl;
}
}