c ++ atm保存平衡重新打开程序

时间:2017-02-24 12:16:40

标签: c++

我已经开始使用这种非常简单的ATM类型的程序了。 我是一个初学者,如果你看到做某些事情的方法非常有效,那就表现出一些怜悯。

即使我重新打开程序,我也需要帮助找到保存存储余额值的方法。因为现在我每次重新打开这个程序时都会创造一个新的“长双重平衡”;变量,它重置了以前使用该程序的值。

例如,假设我使用该程序并存入500欧元。那么余额值将是500欧元。我关闭程序,重新打开,现在一切都重置了。我怎样才能这样做,每当我重新打开程序时,之前的余额值仍然存在并且不会被重置?正如我所说,有很多不同的更有效的方法可以做到这一点,但作为一个初学者,这是我能想到的。谢谢!

#include <iostream>
#include <string>
#include <stdlib.h>
#include <conio.h>
using namespace std;

// Functions
void login();
void menu();
void displayBalance();
void deposit();
void withdraw();
void logout();

// Variables
string name = "";
long double balance;

/* ########################## */

int main(){
    login();
}

void login(){

    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#   Bank/Database  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#  Made by: Fotan  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << endl << endl << endl;

    // Ask user for name and password
    cout << "Login:" << endl;
    cout << " - Enter your name: ";
    getline(cin, name);

    int pw = 0;

    do{
        cout << " - Enter password: ";
        cin >> pw;
    }while(pw != 1234);
    menu();

}

void menu(){
    system("CLS");
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#   Bank/Database  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#  Made by: Fotan  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << endl << endl << endl;

    int choice = 0;

    do{
    cout << "Welcome " << name << "!" << endl;
    cout << " 1 - Display Balance" << endl;
    cout << " 2 - Deposit" << endl;
    cout << " 3 - Withdraw" << endl;
    cout << " 4 - Log out" << endl;
    cin >> choice;
    }while(choice > 4 || choice < 1);

    switch(choice){
        case 1:
            displayBalance();
            break;
        case 2:
            deposit();
            break;
        case 3:
            withdraw();
            break;
        case 4:
            logout();
            break;
    }
}

void displayBalance(){
    system("CLS");
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#   Bank/Database  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#  Made by: Fotan  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << endl << endl << endl;

    cout << "Balance: EUR" << balance << endl;
    cout << " - Press any key to continue..." << endl;
    getch();
    menu();

}

void deposit(){
    system("CLS");
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#   Bank/Database  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#  Made by: Fotan  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << endl << endl << endl;

    long double amount = 0;

    cout << "Enter amount to deposit: ";
    cin >> amount;

    balance += amount;
    cout << "Amount deposited: " << amount << endl;
    cout << " - Press any key to continue..." << endl;
    getch();
    menu();
}

void withdraw(){

}

void logout(){
    system("CLS");
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#   Bank/Database  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << "\t" "\t" "#  Made by: Fotan  #" << endl;
    cout << "\t" "\t" "####################" << endl;
    cout << endl << endl << endl;

    cout << "Thank you " << name << " for using our services." << endl;
    cout << "You have been logged out!" << endl;
}


/*             __
             <(o )___
              ( ._> /
               `---'    */

1 个答案:

答案 0 :(得分:0)

变量不是&#34;自动&#34;程序退出时仍然存在 因此,您需要实现一种机制,将您的balance变量(以及其他变量)保存到磁盘上。

我不会考虑这个宠物项目的真实数据库,所以你可以做的最基本的事情是将balance数量写入并读取到文件(更具体地说是以二进制模式打开的文件)。

我建议你使用c ++ std :: fstream派生,你可以找到一个教程here