C++写简易ATM

.现阶段实现功能:创建一个用户存放在文件里,基础信息:姓名、账户、金额;

取钱;

存钱;

销户;(创建新用户覆盖原内容。)

头文件:

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值