2016-02-26 44 views
0

您好,我一直在研究一个C++银行应用程序,该应用程序应该能够拥有所有相关字段的多个账户。我所遇到的几个问题:C++银行应用程序没有持有多个账户

  • 当显示在显示器或ShowInfo功能的帐户信息,第一个名字的第一个字母,中间最初不会显示出来。
  • 创建帐户时,只能搜索最近的帐户并显示在显示数据中。我知道需要一个数组才能实现,我只是不确定是否正确实施。

感谢您的帮助。任何输入是赞赏!

#include <stdlib.h> 
#include <conio.h> 
#include <iostream> 
#include <string> 

using namespace std; 

class BankAccount{ 
    double Balance = 0.0; 
    char ans; 
    public:struct Name{ 
     char Last_Name[50]; 
     char First_Name[50]; 
     char Middle_Initial[5]; 
    }Name; 
    public:struct Account{ 
     char Type[1]; 
     int Account_Number; 
    }Account; 
    public: 
    void CreateAccount(); 
    void Withdraw(); 
    void Deposit(); 
    void Display(); 
    void ShowInfo(); 
    int Menu(); 

}; 

void BankAccount::CreateAccount(){ 
    do 
    { 
     cout << "\nEnter account number: "; 
     cin >> Account.Account_Number; 
     cout << "\nEnter the last name for the account: "; 
     cin.ignore(); 
     cin.getline(Name.Last_Name, 50); 
     cout << "\nEnter the first name for the account: "; 
     cin.ignore(); 
     cin.getline(Name.First_Name, 50); 
     cout << "\nEnter the Middle initial for the account: "; 
     cin.ignore(); 
     cin.getline(Name.Middle_Initial, 5); 
     cout << "\nEnter the type of account (C/S) : "; 
     cin >> Account.Type; 
     cout << "\nEnter the initial balance of the account: "; 
     cin >> Balance; 
     cout << "\n\nAccount Created."; 
     cout << "\n\nCreate new account? (Y/N) : "; 
     cin >> ans; 

     while (ans != 'Y' && ans != 'N'){ 
      cout << "Invalid input. Create new account? (Y/N) : "; 
      cin >> ans; 
     } 
     cout << endl; 
    } while (ans != 'N'); 
}; 
void BankAccount::Withdraw(){ 
    int actNum; 
    double amount; 
    cout << "Enter the account number for the account that you wish to withdraw funds: "; 
    cin >> actNum; 
    if (actNum == Account.Account_Number){ 
     cout << "Enter the amount you would like to withdraw: "; 
     cin >> amount; 
     Balance = Balance - amount; 
    } 
    else if (actNum != Account.Account_Number){ 
     cout << "No account found under that number! Try again!"; 
    } 

} 
void BankAccount::Deposit(){ 
    int actNum; 
    double amount; 
    cout << "Enter the account number for the account that you wish to deposit funds: "; 
    cin >> actNum; 
    if (actNum == Account.Account_Number){ 
     cout << "Enter the amount you would like to deposit: "; 
     cin >> amount; 
     Balance = Balance + amount; 
    } 
    else if (actNum != Account.Account_Number){ 
     cout << "No account found under that number! Try again!"; 
    } 
} 
void BankAccount::Display(){ 
    int actNum; 
    cout << "Enter the account number for the account that you wish to display account information for: "; 
    cin >> actNum; 
    if (actNum == Account.Account_Number){ 
     cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl; 
     cout << "Account Number: " << Account.Account_Number << endl; 
     cout << "Account Type (Checking/Savings): " << Account.Type << endl; 
     cout << "Account Balance: $" << Balance << endl; 
    } 
    else if (actNum != Account.Account_Number){ 
     cout << "No account found under that number! Try again!"; 
    } 

} 
void BankAccount::ShowInfo(){ 
    cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl; 
    cout << "Account Number: " << Account.Account_Number << endl; 
    cout << "Account Type (Checking/Savings): " << Account.Type << endl; 
    cout << "Account Balance: $" << Balance << endl;  

} 

int main(int argc, char *argv){ 
    BankAccount ob; 
    char ch; 

    cout << "Welcome to Console Banking Application V 1.0!"; 
    cout << "\nSelect an item from the list below by entering the corresponding letter."; 
    do{ 
     cout << "\n\n A. Create Account \n B. Withdraw \n C. Deposit \n D. Show Account Details \n\n Q. Exit Application\n\n"; 
     ch = ob.Menu(); 
     switch (ch){ 
     case 'A': 
     case 'a': ob.CreateAccount(); 
      ob.ShowInfo(); 
      break; 
     case 'B': 
     case 'b': ob.Withdraw(); 
      break; 
     case 'C': 
     case 'c': ob.Deposit(); 
      break; 
     case 'D': 
     case 'd': ob.Display(); 
      break; 
     case 'Q': 
     case 'q': ob.ShowInfo(); 
        exit(1); 
      break; 
     } 

    } while (1); 
} 
int BankAccount::Menu(){ 
    char ch; 
    cout << "Select an option: "; 
    cin >> ch; 
    return ch; 
} 
+0

为什么*不会*您只能访问一个帐户?您阅读帐号,然后检查它是否等于'Account.Account_Number',它只是一个数字。如果不相等,则打印错误消息。 – immibis

+1

为什么使用char数组? ['std :: string'](http://www.cplusplus.com/reference/string/string/)你包含'',为什么不使用它?此外'公共'不需要经常... – Blacktempel

+0

关于如何使用数组来保存帐户信息的任何提示? – AddieCaddy

回答

1

简单的答案是:你只能有一个银行帐户。

如果你看看你的主要功能,你创建一个,只有一个 BankAccount。正如您可能会猜到的,一个BankAccount不能用于表示多个BankAccount(不考虑SoA)。因此,您需要BankAccounts的数组。它是这样的:

BankAccount obs[10]; 

现在你有10 BankAccounts,没有更多。所以每次你想创建一个新的BankAccount时,只要确保你在当前未使用的数组中的BankAccount上创建它。

obs[0].CreateAccount(); 
obs[0].ShowInfo(); 
obs[0].Deposit(); 
//Make a new account 
obs[1].CreateAccount(); 
... 

再进一步,让我们考虑一下,你有10个BankAccounts,只有10这是不是很广泛,现在是不是事实?什么是只有10个账户可用的银行?在适当的时候可能会歇业。

天真的解决方案是只需添加更多。 50,100,甚至1000.但是你真的想每次都回去更新这个数字吗?那太乏味了。

幸运的是,有一个方便的容器,我们可以使用:

#include <vector> 
... 
std::vector<BankAccount> obs; 
... 

这基本上是在需要时自动扩展自身的数组。我不会详细介绍如何使用载体,因为我相信你自己可以很容易地学会这样做。不过,我会给你留下这个link,所以你知道你可以从哪里开始。