2015-12-08 65 views
-3

我想顯示的界面,詢問用戶是否他們想用checkings或儲蓄賬戶,然後當他們選擇把他們帶到我的通用顯示菜單。但我不太清楚如何設置它。我被告知要製作帳戶對象並使用指針,但正如您所看到的,我被卡住了。如果任何人都可以告訴我如何做到這一點,我將不勝感激。UI爲建立一個支票賬戶和儲蓄賬戶爲我的銀行項目

這裏是我的代碼:

#ifndef ACCOUNT_H 
#define ACCOUNT_H 

class Account 
{ 
private: 
    double balance; // Account balance 
    double interestRate; // Interest rate for the period 
    double interest; // Interest earned for the period 
    int transactions; // Number of transactions 

public: 
    Account(double iRate = 0.045, double bal = 0) 
    { balance = bal; 
    interestRate = iRate; 
    interest = 0; 
    transactions = 0; } 

    void setInterestRate(double iRate) 
    { interestRate = iRate; } 

    void makeDeposit(double amount) 
    { balance += amount; transactions++; } 

    bool withdraw(double amount); // Defined in Account.cpp 

    void calcInterest() 
    { interest = balance * interestRate; balance += interest; } 

    double getInterestRate() const 
    { return interestRate; } 

    double getBalance() const 
    { return balance; } 

    double getInterest() const 
    { return interest; }  

    int getTransactions() const 
    { return transactions; } 

}; 

#endif 

int main() 
{ 
    Account Savings; 
    Account Checkings; 
    Account *ptr; 

    Account savings; // Savings account object 

    char choice; // Menu selection 

    // Set numeric output formatting. 
    cout << fixed << showpoint << setprecision(2); 

    do 
    { 
    // Display the menu and get a valid selection. 
    displayMenu(); 

    cin >> choice; 

    while (toupper(choice) < 'A' || toupper(choice) > 'G') 
    { 
     cout << "Please make a choice in the range " << "of A through G:"; 
     cin >> choice; 
    } 

    // Process the user's menu selection. 
    switch(choice) 
    { 
     case 'a': 
     case 'A': cout << "The current balance is $"; 
     cout << savings.getBalance() << endl; 
     break; 
     case 'b': 
     case 'B': cout << "There have been "; 
     cout << savings.getTransactions() << " transactions.\n"; 
     break; 
     case 'c': 
     case 'C': cout << "Interest earned for this period: $"; 
     cout << savings.getInterest() << endl; 
     break; 
     case 'd': 
     case 'D': makeDeposit(savings); 
     break; 
     case 'e': 
     case 'E': withdraw(savings); 
     break; 
     case 'f': 
     case 'F': savings.calcInterest(); 
     cout << "Interest added.\n"; 
    } 
    } while (toupper(choice) != 'G'); 
return 0; 
} 

void displayMenu() 
{ 
    cout << "\n Welcome to The Bank \n"; 
    cout << "-----------------------------------------\n"; 
    cout << "A) Display the account balance\n"; 
    cout << "B) Display the number of transactions\n"; 
    cout << "C) Display interest earned for this period\n"; 
    cout << "D) Make a deposit\n"; 
    cout << "E) Make a withdrawal\n"; 
    cout << "F) Add interest for this period\n"; 
    cout << "G) Exit the program\n\n"; 
    cout << "Enter your choice: "; 
} 

void makeDeposit(Account *acct) 
{ 
    double dollars; 

    cout << "Enter the amount of the deposit: "; 

    cin >> dollars; 
    cin.ignore(); 

    acct.makeDeposit(dollars); 
} 

void withdraw(Account *acct) 
{ 
    double dollars; 

    cout << "Enter the amount of the withdrawal: "; 

    cin >> dollars; 
    cin.ignore(); 

    if (!acct.withdraw(dollars)) 
    cout << "ERROR: Withdrawal amount too large.\n\n"; 
} 
+0

我建議投資 ''在你的描述中。看起來像一個長句。 –

+0

您可以通過'之開關語句之前choice'轉換'爲大寫或小寫節省了大量的代碼。請參閱'std :: toupper'或'std :: tolower'。 –

回答

0

嘗試是這樣的:

char displayAccountSelectionMenu(); 
char displayAccountActionMenu(); 
void makeDeposit(Account *acct); 
void withdraw(Account *acct); 

int main() 
{ 
    Account Savings; 
    Account Checkings; 
    Account *acct = NULL; 

    char choice; // Menu selection 

    do 
    { 
     // Display the menu and get a valid selection. 
     choice = displayAccountSelectionMenu(); 

     // Process the user's menu selection. 
     switch (choice) 
     { 
      case 'A': 
       acct = &Savings; 
       break; 

      case 'B': 
       acct = &Checkings; 
       break; 

      case 'C': 
       return 0; 
     } 

     // Set numeric output formatting. 
     cout << fixed << showpoint << setprecision(2); 

     do 
     { 
      // Display the menu and get a valid selection. 
      choice = displayAccountActionMenu(); 

      // Process the user's menu selection. 
      switch (choice) 
      { 
       case 'A': 
        cout << "The current balance is $" 
         << acct->getBalance() << endl; 
        break; 

       case 'B': 
        cout << "There have been " 
         << acct->getTransactions() 
         << " transactions." << endl; 
        break; 

       case 'C': 
        cout << "Interest earned for this period: $" 
         << acct->getInterest() << endl; 
        break; 

       case 'D': 
        makeDeposit(acct); 
        break; 

       case 'E': 
        withdraw(acct); 
        break; 

       case 'F': 
        acct->calcInterest(); 
        cout << "Interest added." << endl; 
        break; 

       case 'G': 
        break; 

       case 'H': 
        return 0; 
      } 
     } 
     while (choice != 'G'); 
    } 
    while (true); 

    return 0; 
} 

char displayAccountSelectionMenu() 
{ 
    char choice; 

    cout << "\n Welcome to The Bank \n"; 
    cout << "-----------------------------------------\n"; 
    cout << "Select an account:\n"; 
    cout << "A) Savings\n"; 
    cout << "B) Checking\n"; 
    cout << "C) Exit the program\n\n"; 
    cout << "Enter your choice: "; 

    cin >> choice; 
    choice = toupper(choice); 

    while ((choice < 'A') || (choice > 'C')) 
    { 
     cout << "Please make a choice in the range of A through C:"; 
     cin >> choice; 
     choice = toupper(choice); 
    } 

    return choice; 
} 

char displayAccountActionMenu() 
{ 
    char choice; 

    cout << "\n Welcome to The Bank \n"; 
    cout << "-----------------------------------------\n"; 
    cout << "Select an action:\n"; 
    cout << "A) Display the account balance\n"; 
    cout << "B) Display the number of transactions\n"; 
    cout << "C) Display interest earned for this period\n"; 
    cout << "D) Make a deposit\n"; 
    cout << "E) Make a withdrawal\n"; 
    cout << "F) Add interest for this period\n"; 
    cout << "G) Select a different account\n"; 
    cout << "H) Exit the program\n\n"; 
    cout << "Enter your choice: "; 

    cin >> choice; 
    choice = toupper(choice); 

    while ((choice < 'A') || (choice > 'H')) 
    { 
     cout << "Please make a choice in the range of A through H:"; 
     cin >> choice; 
     choice = toupper(choice); 
    } 

    return choice; 
} 

void makeDeposit(Account *acct) 
{ 
    //... 
} 

void withdraw(Account *acct) 
{ 
    // ... 
} 
+0

錯誤錯誤C3861: 'displayAccountActionMenu':標識符找不到\t升:\銀行賬戶項目\銀行賬戶項目\ main.cpp中銀行賬戶項目 – user3481999

+0

錯誤錯誤C3861: 'displayAccountSelectionMenu':標識未發現\t升:\銀行賬戶項目\銀行賬戶項目\ main.cpp中銀行賬戶項目 收到這些錯誤與代碼的任何解決? – user3481999

+0

很明顯,你將不得不宣佈'displayAccountSelectionMenu()'和'displayAccountActionMenu()'使用它們之前。我給你實現他們,認爲你會明白這一點。你沒有顯示任何你的函數聲明。我已經更新了我的答案以包含聲明。 –