为什么数组对象类的所有元素都重复上次输入的数据C++

Why do all elements for the array object class repeat last entered data C++

本文关键字:输入 C++ 数据 元素 对象 数组 为什么      更新时间:2023-10-16

我有一个类BankAccount,它有一个由BankAccount对象组成的数组。每个对象都有每个客户的名称、余额、银行帐户类型等等。实现这一点的方式是通过BankAccount*客户[10]作为类私有字段member。然后用所选构造函数生成的对象填充客户。构成每个对象的每一条数据都是由用户输入的,这些对象对客户来说是一个元素。在display()中,数据是输出的,但问题是最后一个输入的对象会为每个元素重复给客户。为什么这样重复?任何帮助或建议都将是伟大的!

BankAccount::BankAccount() :  customers()
{
setAcctAmt();
}
void BankAccount::work()
{
for (int x = 0; x < accountAmount; x++)
{
    bool t = true;
    string theName, sT;
    double balance, iRate;
    cout << "Enter the name for account " << x + 1 << endl;
    cin >> theName;
    while (t)
    {
        if (t == false)
            exit;
        cout << "Bank Account type: Checking or Saving" << endl;
        cin >> sT;
        string s;
        s = sT.substr(0, 1);
        if (s == "c")
        {
            sT = "Checking Account ";
            cout << "Input checking balance: ";
            cin >> balance;
            iRate = 0.02;
            makeAcctNum();
            constAcct(theName, balance, iRate, sT); // This is where customers is constructed and data is imput from parameters
            t = false;
        }
        else if (s == "s")
        {
            sT = "Savings Account ";
            cout << "Input saving balance: ";
            cin >> balance;
            iRate = 0.07;
            makeAcctNum();
            constAcct(theName, balance, iRate, sT); // The constructed object
            t = false;
        }
        else
            cout << "Error, enter checking or saving!" << endl;
    }
}
display(); // This is the display function to display all constructed objects of customers
}
// This is the display function
void BankAccount::display()
{
for (int i = 0; i < accountAmount; i++)
{
    cout << customers[i]->getAcctNum() << " " << customers[i]->getName() << " " << customers[i]->getType() << " " << customers[i]->getRate()
                    << " " << customers[i]->getBalance();
}
}
// This is the constructor that will build each customers element as customer data
BankAccount::BankAccount(string* nam, int* acctNumber, double* balanc, double* rat, string* typ)
{
rate = rat;
account = acctNumber;
name = nam;
type = typ;
bal = balanc;
}
void BankAccount::deleteStuff()
{
delete name, type, bal, rate, account, customers;
}
// This constructs each customers element
void BankAccount::constAcct(string n, double ba, double r, string t)
{
nameS = n;
balD = ba;
rateD = r;
typeS = t;
name = &nameS;
account = &acctNumber;
rate = &rateD;
bal = &balD;
type = &typeS;
for (int i = 0; i < accountAmount; i++)
{
    BankAccount* b = new BankAccount(name, account, bal, rate, type);
    customers[i] = b;
}
}

基于此注释和代码行:

// This is where customers is constructed and data is imput from parameters    
constAcct(theName, balance, iRate, sT);

您的意图似乎是使用constAcct方法创建一个新帐户。看看这个方法:

for (int i = 0; i < accountAmount; i++)
{
    BankAccount* b = new BankAccount(name, account, bal, rate, type);
    customers[i] = b;
}

您正在使用由相同参数构建的new BankAccount重写customers数组中的所有条目(无论上一个参数是什么)。

要解决这个问题,你应该用这样的东西替换上面的循环:

customers[lastAccountIndex++] = new BankAccount(name, account, bal, rate, type);

这里lastAccountIndex是一个变量,用来跟踪已经添加的帐户的数量。