0

所有代码都符合我的喜好,我的客户名称和初始余额没有被存储,因为当函数运行时,它只输入 0 作为初始余额。我犯了一个简单的错误吗?主要的。cpp

#include <iostream>
#include <sstream>
#include <string>
#include "Account.h"

using namespace std;

int main()
{
    char Redo;
    string CustomerName;

do
{
    float InitialBalance = -1;
    float balance1 = 0;
    float balance2 = 0;


    Account Account;
    Account.CreditAccount (balance1, InitialBalance);
    Account.DebitAccount (balance2, InitialBalance);
    Account.DisplayBalance (CustomerName, balance1, balance2);

    //Asks user if they want redo the program
    cout << "Would you like to redo the program?\n";
    cout << "Please enter Y or N: \n \n";
    cin >> Redo;
}while(Redo == 'Y' || Redo == 'y');


char exitchar; //Exit's the program.
cout << "\nPress any key and <enter> to exit the program.\n";
cin >> exitchar;

return 0;
}

帐户.h

using namespace std;

class Account {
public:
    float balance1;
    float balance2;
    string CustomerName;
    float InitialBalance;
    float CreditAccount(float& balance1, float InitialBalance);
    float DebitAccount(float& balance2, float InitialBalance);
    float DisplayBalance(string CustomerName, float balance1, float balance2);
    Account (void);


    Account(float balance)
    {
        SetInitialBalance(balance);
    }
    void Account::SetInitialBalance(float balance)
    {
        if(balance >= 0)
        {
            InitialBalance = balance;
        }
        else
            cout << "Error! Initial Balance cannot be less than 0." << endl;
    }
};

Account::Account(void)
{
string CustomerName;

cout << "Your Account Machine" << endl;
cout << "Please enter your last name." << endl;
cin >> CustomerName;
while(InitialBalance < 0)
{
cout << "Please enter your account balance. No Commas." << endl;
cin >> InitialBalance;
if(InitialBalance < 0)
    cout << "Error account balance must be positive." << endl;
}
}

float Account::CreditAccount(float& balance1, float InitialBalance)
    {
        float CreditInput = -1;
        while(CreditInput<0){
        cout << "Would you like to credit the account? Enter the amount you would like to credit." << endl;
        cin >> CreditInput;
        if (CreditInput<0)
            cout << "Credit must be positive." << endl;
        }
        balance1 = (CreditInput + InitialBalance);
        return balance1;
    }
float Account::DebitAccount(float& balance2, float InitialBalance)
    {
        float DebitInput = 0;
        while((InitialBalance - DebitInput)<0){
        cout << "Would you like to debit the account? Enter the amount you would like to debit." << endl;
        cin >> DebitInput;
        if((InitialBalance-DebitInput)<0)
            cout << "You cannot debit more than you have avalaible." << endl;
        }
        balance2 = (InitialBalance - DebitInput);
        if( DebitInput > InitialBalance)
        {
        cout << "Debit amount exceeds account balance." << endl;
        return 0;
        }
        else 
            return balance2;
    }
float Account::DisplayBalance(string CustomerName, float balance1, float balance2)
    {
        cout << "Customer Name: " << CustomerName << endl;
        cout << "Account Balance for credited account: " << balance1 << endl;
        cout << "Account Balance for debited account: " << balance2 << endl;
        return 0;
    }
4

2 回答 2

0

问题是您已经声明了一个新的局部变量CustomerName。您正在接受输入,但不接受成员变量。

Account::Account(void)
{
    string CustomerName; // This is different from the member variable
    .....
    cin >> CustomerName;  // You are taking input to the local variable
                          // but not to the member variable
于 2013-09-18T22:37:59.017 回答
0

我认为你的问题是,如果类成员发生变化,“main”函数中的变量会发生变化。

int main()
{
    char Redo;
    string CustomerName;

    do
    {
      float InitialBalance = -1;
      float balance1 = 0;
      float balance2 = 0;

      Account Account;
      // [..]
      Account.DisplayBalance (CustomerName, balance1, balance2);
      // [...]
    }while(Redo == 'Y' || Redo == 'y');
}

但是,如果您创建一个新的Account并且从未设置真实帐户值,它们将永远不会被存储。

如果您在设置为某个值的任何地方创建局部变量,那么整个程序就没有多大意义。一个例子:

Account::Account(void)
{
string CustomerName;

// [..]
cin >> CustomerName;
// [..]
}

这会在构造函数中创建一个非真实类成员的局部变量,并将客户名称存储到此局部变量中。真正的类成员“CustomerName”永远不会被设置,并且在构造函数准备好后局部变量将被删除。

更好的做法:

Account::Account(void)
{    
string cname;
// [..]
cin >> cname;
this->CustomerName = cname;
// [..]
}

如果我做错了什么,我很抱歉:)

于 2013-09-18T23:18:04.027 回答