为什么数组对象类的所有元素都重复上次输入的数据C++
Why do all elements for the array object class repeat last entered data C++
我有一个类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
是一个变量,用来跟踪已经添加的帐户的数量。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 如何只允许用户输入正整数
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 输入中的字符串数未知(以字母表示)
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type