-2

これが私のプログラムの完全なコードであり、以下に述べる問題です。

このコードは、顧客の預金、残高の確認、口座からのお金の引き出しを処理する「仮想 ATM マシン」プログラムからのものです。お金を入金すると、入金されたと表示されます..しかし、「customer_actions()」は顧客のメインメニューであり、その画面に戻って残高を確認するオプションを選択すると、ゼロと表示されます。

これは、値が前の関数から更新されなかったことを意味します。ユーザーが入金額を入力したときに更新するようにしたい:/

問題は預金機能にあると思います。しかし、私はあなたの処分のために完全なプログラムを投稿しました.

どうもありがとう:)

ヘッダファイル:

#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)

4

1 に答える 1

1

bankAccountある関数から別の関数に渡す必要があります。bankAccount&これは、同じオブジェクトが渡されるように、関数のパラメーターとして aを渡すことで実行できます。関数を変更する関数は、関数のスコープを離れると破棄される銀行口座をbankAccount現在使用しています!local

アプリケーションの単純さを考えると、代わりにグローバルを使用するのは醜いハックかもしれません。

于 2013-05-23T13:50:58.927 に答える