.现阶段实现功能:创建一个用户存放在文件里,基础信息:姓名、账户、金额;
取钱;
存钱;
销户;(创建新用户覆盖原内容。)
头文件:
ifndef __ZY4_H_
#define __ZY4_H_
#include <iostream>
#include <cstring>
using namespace std;
class A {
public:
void QuKuan();//取款
void CunKuan();//存款
void XiaoHu();//销户
void KaiHu();//开户
protected:
string name;
int name1 = 1000;
double a = 0;
double b;
};
#endif
.cpp
#include <iostream>
#include <fstream>
#include "zy4.h"
using namespace std;
void A::KaiHu()
{ cout << "登入 请输入姓名:" <<endl;
cin>>name;
ofstream out("out.txt");//写东西进入out文件
if (out.is_open())
{
out << "姓名:" << name
<< "账户 :" << ++name1
<< "金额 :" << a << endl;
}
char buffer[256];//读文件
ifstream in("out.txt");
if (! in.is_open())//判断文件是否成功打开;
{ cout << "Error opening file"; exit (1); }
while (!in.eof() )//判断文件是否读取到末尾
{
in.getline (buffer,100);//读取一行;
cout << buffer << endl;
}
}
void A::CunKuan()
{
double c;
cout << "存钱:" << endl;
cin>>c;
b=a+c;
fstream f1("out.txt",ios::out | ios::app);//以追加的方式写文件
f1 << "存钱:" << c
<< "余额 :" << b << endl;
f1.close();
}
void A::QuKuan()
{
double d;
cout << "取钱:" <<endl;
cin>>d;
b -= d;
if(b>0){
fstream f1("out.txt",ios::out | ios::app);//以追加的方式写文件
f1<< "取钱:" << d
<< "余额 :" << b << endl;
f1.close();
}
else
cout << "异常" <<endl;
}
void A::XiaoHu()
{
fopen("out.txt","w+");
}
int main(int argc, char *argv[])
{
A atm;
int x;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 开户" << endl;
cout << "2. 存款" << endl;
cout << "3. 取款" << endl;
cout << "4. 销户" << endl;
cout << "5. 退出" << endl;
cin >> x;
switch (x) {
case 1:
A().KaiHu();//用后及删除
break;
case 2:
atm.CunKuan();
break;
case 3:
atm.QuKuan();
break;
case 4:
atm.XiaoHu();
break;
case 5:
exit(0);
default:
cout << "无效的选择,请重试!" << endl;
}
}
return 0;
}