当我运行当前程序时,我遇到了一些麻烦。我可以在此帐户余额中添加任意数量的交易,但是当我超过第一个数组值并输入信息时,它会显示一个内存位置。我的代码是什么问题,然后在这里。
for(int i = 0; i < ACCTS; ++i)
{
do
{
debitCredit = accounts[x].operator+=(accounts[x]);
cout << "Account Balance is:$" << debitCredit << endl;
cout << "Enter " << QUIT << " to stop transactions." << endl;
cin >> selection;
}while(selection != QUIT);
++x;
}
此处的源代码如下:
//Alex Weir
// Case 2 Chapter 9
#include <iostream>
#include <iomanip>
using namespace std;
class BankAccount
{
friend ostream& operator<<(ostream&, const BankAccount&);
friend istream& operator>>(istream&, BankAccount&);
private:
int accountNum;
int increaseAccountNum;
double accountBal;
double annualIntRate;
double debitCredit;
public:
BankAccount();
BankAccount(int,int,double,double,double);
int operator==(const BankAccount&);
void operator<(const BankAccount&);
void operator>(const BankAccount&);
double operator+=(BankAccount);
int operator+(BankAccount);
void displayAccounts();
};
double BankAccount::operator+=(BankAccount account)
{
cout << "How much money would you like to deposit or withdraw?" << endl <<
" Enter a negative amount to withdraw." << endl;
cin >> debitCredit;
debitCredit = debitCredit + account.accountBal;
return debitCredit;
}
int BankAccount::operator+(BankAccount account)
{
int increment;
int accountNum = increment + account.accountNum;
return accountNum;
}
void BankAccount::operator>(const BankAccount& accounts)
{
if(accountBal > accounts.accountBal)
{
cout << "Account Balance is greater than another account." << endl;
}
else
{
cout << "Account Balance is less than another account." << endl;
}
}
void BankAccount::operator<(const BankAccount& accounts)
{
if(accountBal < accounts.accountBal)
{
cout << "Account Balance is less than another account." << endl;
}
else
{
cout << "Account Balance is greater than another account." << endl;
}
}
BankAccount::operator==(const BankAccount& acctNumb)
{
int isTrue = 0;
if(accountNum == acctNumb.accountNum)
isTrue = 1;
return isTrue;
}
ostream& operator << (ostream& out, const BankAccount& Accounts)
{
cout << endl;
out << "Account #" << Accounts.accountNum << endl;
out << "Account Balance:$" << Accounts.accountBal << endl;
out << "Account interest rate: " << Accounts.annualIntRate << endl;
cout << endl;
return out;
}
istream& operator >> (istream& in, BankAccount& Accounts)
{
cout << "Enter Account # " << endl;
in >> Accounts.accountNum;
cout << "Enter Account Balance: $";
in >> Accounts.accountBal;
cout << endl << "Enter Account Interest Rate: " << endl;
in >> Accounts.annualIntRate;
cout << endl;
return in;
}
BankAccount::BankAccount()
{
accountNum = 0;
accountBal = 0;
annualIntRate = 0;
increaseAccountNum = 0;
debitCredit = 0;
}
BankAccount::BankAccount(int acctNum, int increment, double acctBal, double intRate, double debCred)
{
accountNum = acctNum;
accountBal = acctBal;
annualIntRate = intRate;
increaseAccountNum = increment;
debitCredit = debCred;
}
void BankAccount::displayAccounts()
{
cout << "Account # " << accountNum << endl;
cout << "Account balance:$" << accountBal << endl;
cout << "Account Interest Rate: " << annualIntRate << endl;
cout << endl;
}
int main()
{
const int ACCTS = 5;
const int QUIT = 0;
int x, selection;
double debitCredit = 0.0;
BankAccount accounts[ACCTS];
cout << "Enter Bank account information for: " << ACCTS << " accounts." << endl;
for(x = 0; x < ACCTS; ++x)
{
accounts[x].displayAccounts();
}
for(int i = 0; i < ACCTS; ++i)
{
do
{
debitCredit = accounts[x].operator+=(accounts[x]);
cout << "Account Balance is:$" << debitCredit << endl;
cout << "Enter " << QUIT << " to stop transactions." << endl;
cin >> selection;
}while(selection != QUIT);
++x;
}
for(x = 0; x < ACCTS; ++x)
{
accounts[x].displayAccounts();
}
/*for(x = 0; x < ACCTS; ++x)
{
cout << "Entry #" << (x + 1) << endl;
cin >> accounts[x];
cout << accounts[x];
}
double transactions;
for(x = 0; x < ACCTS; ++x)
{
}*/
好了,现在我已经摆脱了x我继续把我的变量改为“我”现在,我通过5个数组元素,但是我想从数组0开始然后经历循环多次,因为我在我点击“停止”之后想要(仍然在数组元素0处播放余额)或者在给定机会时我想要移动到第一个数组元素并通过它,添加和子追踪,因为我觉得nessacary并重复这个过程,直到我对它很好。将数组的元素变量重新设置为“i”不会执行此操作并从最后使用的数组元素继承。 系统( “暂停”); 返回0; }
答案 0 :(得分:2)
这里有几件事可能有问题:
debitCredit = accounts[x].operator+=(accounts[x]);
i
代替x
? x
的值是否已初始化(代码未显示)?accounts[x]
?实际上,你的价值翻了一倍。accounts
数组是否足够大?它是如何初始化的?查看您发布的额外代码后:丢失x
并使用i
代替。您超越了accounts
数组的范围。