C ++ ATM机 - 一点帮助?

时间:2013-05-23 13:47:41

标签: c++ class compilation

以下是我的计划的完整代码和下面提到的问题。

此代码来自我的“虚拟ATM机”程序,该程序处理客户存款,核对余额以及从其帐户中提取资金。当我存钱时,它显示它已存入..但“customer_actions()”是客户的主菜单,当我返回该屏幕并选择检查余额的选项时,它显示为零。

这意味着未从上一个函数更新值。当用户输入存款金额时,我会更新它:/

问题在于存款功能,我猜测。但我发布了完整的程序供您处理。

非常感谢你们:)

HEADER FILE:

#ifndef bank
#define bank


using namespace std;


class bankAccount
{
    public:
        int accNo;
        int password;

        double balance;
        double withdrawamt;
        double depositamt;

        char name[20];
        char address[40];
        char username[10];

    public:

        double checkbalance();
        double deposit();
        double withdraw();


   /* public:
           bankAccount()
           {
              balance = 0;
           }    
*/
};


#endif

到目前为止完成整个计划:

#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include "bank.h"

using namespace std;
using std::string;

int logincheck (int ,int );
double checkbal (double );
int mainmenu(int );
int customer_func();
int officer_func();
int enroll();
int customer_actions();



main() {

   double balance = 0, deposit = 0, withdraw = 0;
   int atmno;
   bankAccount b;


    system("cls");
    cout << endl;
    cout << "**************** Please select an ATM to continue *********************  \n"<<endl;;
    cout << "-----------------------------------------------------------------------  \n";
    cout << "|          ATM 1                  ATM 2                  ATM 3        |  \n";
    cout << "-----------------------------------------------------------------------  \n"<<endl;

    cout << "Enter ATM number: ";
    cin >> atmno;

    switch(atmno)
    {
        case 1: mainmenu(atmno); break;
        case 2: mainmenu(atmno); break;
        case 3: mainmenu(atmno); break;
    }
}

   int mainmenu(int atmno)
   {

   int mainselection;
   system("cls");
   // Menu
   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                               ATM Booth: " << atmno << "                            | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;

   cout << "1) Customer Login    : Press 1" << endl;
   cout << "2) Officer Login     : Press 2" << endl;
   cout << "\nEnter option: ";
   cin >> mainselection;

   switch(mainselection)
   {
        case 1: customer_actions(); break;
        case 2: officer_func(); break;
   }

}

    int customer_func()
    {
        int usr;
        int pss;
        bankAccount b;


           system("cls");

           cout << " -----------------------------------------------------------------------  \n";
           cout << "|                        AUTOMATED TELLER MACHINE                       | \n";
           cout << " -----------------------------------------------------------------------  \n";
           cout << "|           Please enter username and password to continue.             | \n";
           cout << "|                                                                       | \n";
           cout << "|                                                                       | \n";
           cout << "|                                                                       | \n";
           cout << " -----------------------------------------------------------------------  \n\n";

           cout << "Username: ";
           cin >> usr;

           cout << "Password: ";

           char c = ' ';
           while(c != 13) //Password masking with *
             {
              c = getch();
              pss += c;
              cout << "*";
              }

           cin >> pss;

           //logincheck (usr, pss);


           getch();
        }

/*
int logincheck (int usr, int pss, bankAccount b)
{




}
*/



int officer_func()
{
    int officer_selection;
system("cls");
   // officer menu
   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                              Officer Menu                             | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;

   cout << "1) Enroll new customer    : Press 1" << endl;
   cout << "2) Return home            : Press 2" << endl;
   cout << "\nEnter option: ";
   cin >> officer_selection;


   switch(officer_selection)
   {
        case 1: enroll(); break;
        case 2: main(); break;
   }



}

int enroll()
{
    bankAccount b;

    system("cls");

   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                          Enroll new customer                          | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;



   ofstream myfile;

   b.balance = 0;
   cout << "Please enter customer name: " << endl;
   cin >> b.name;
   cout << "Please enter customer address: " << endl;
   cin >> b.address;
   cout << "Please enter customer Account Number: " << endl;
   cin >> b.accNo;
   cout << "Please enter username: (ONLY Numerals)" << endl;
   cin >> b.username;



  //generatepass(bankAccount& b);



   b.balance = 500000;



   int genpass = (b.accNo + 10);
   b.password = genpass;

   cout << "\nPassword Generated: " << genpass;

   myfile.open ("accounts.txt");
   myfile << b.name << endl << b.address << endl << b.accNo << endl << b.username << endl << genpass << endl << b.balance;
   myfile.close();    

   cout << "\n\nCustomer Account created! Press any key to save account file";
   getch();

   officer_func();

}



int customer_actions()
    {
          bankAccount b;
          int cust_selection;
          system("cls");

   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                              Customer Menu                            | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;

   cout << "1) Check balance    : Press 1" << endl;
   cout << "2) Withdraw Cash    : Press 2" << endl;
   cout << "3) Deposit Cash     : Press 3" << endl;
   cout << "4) Transfer Cash    : Press 4" << endl;
   cout << "5) Return home      : Press 5" << endl;
   cout << "\nEnter option: ";
   cin >> cust_selection;               


   switch(cust_selection)
   {
      case 1: b.checkbalance();   break;
      case 2: b.withdraw(); break;
      case 3: 
           for(int i=0;i<=20;i++)
           {
                   b.deposit(); 
           break;
           }    
      case 4: break;
      case 5: main(); break;   
    }

}


double bankAccount::checkbalance()
{
       system("cls");
       cout << " -----------------------------------------------------------------------  \n";
       cout << "|                              Customer Menu                            | \n";
       cout << " ----------------- ----------------- ----------------- -----------------  \n";

       cout << "\n\nYour balance is: " << balance;

       getch();
       customer_actions();
       return balance;
}


double bankAccount::withdraw()
{
       system("cls");
       cout << " -----------------------------------------------------------------------  \n";
       cout << "|                              Customer Menu                            | \n";
       cout << " ----------------- ----------------- ----------------- -----------------  \n";


       cout << "\n\nEnter Withdraw amount: ";
       cin >> withdrawamt;
       if (balance > withdrawamt)
          balance = (balance - withdrawamt);
       else {
            cout << "\nSorry you do not have enough money to make this transaction!";
            getch();
            customer_actions();
            }

       cout << "\nYou have withdrawn " << withdrawamt << " successfully!";
       cout <<"\nYour balance is: " << balance;

       getch();
       customer_actions();
       return balance;  
}


double bankAccount::deposit()
{


       double amt;

       system("cls");
       cout << " -----------------------------------------------------------------------  \n";
       cout << "|                              Customer Menu                            | \n";
       cout << " ----------------- ----------------- ----------------- -----------------  \n";

       cout << "\n\nYOUR CURRENT BALANCE: " << balance << endl;
       cout << "\nEnter amount to deposit: ";
       cin >> amt;

       balance = (balance + amt);

       cout << "\nAmount depositted successfully!" << endl;
       cout <<"\nYOUR CURRENT BALANCE: " << balance;

       getch();
       return balance;

}

我已经“评论”的代码是我用于测试的代码或者我正在处理的代码,现在不是真的需要(Y)

1 个答案:

答案 0 :(得分:1)

您需要将bankAccount从一个功能传递到另一个功能。这可以通过传递bankAccount&作为函数的参数来完成,以便传递相同的对象。您修改bankAccount的功能当前使用的是local银行帐户,该帐户在剩余功能范围时会被丢弃!

一个丑陋的黑客可能是使用全局而不是考虑应用程序的简单性。