const int CHECK_MAX = 50;
void balanceAccount(double account)
{
double start = 0;
double interest = 0;
double check[CHECK_MAX];
account = (start - check[CHECK_MAX]) * (1 + interest);
cout << account;
}
int main()
{
int sub;
double start = 0;
double interest = 0;
double total = 0;
double check[CHECK_MAX];
void balanceAccount(double);
cin >> start;
for (sub = 0; sub < CHECK_MAX; sub++)
{
cin >> check[sub];
if (check[sub] == 0)
{
cout << "Thank you." << endl;
break;
}
}
cin >> interest;
if (interest == 0)
{
balanceAccount(total);
}
else
{
balanceAccount(total);
}
return 0;
}
此程序的目的是提示用户输入银行帐户的起始余额,然后使用数组,输入单独的支票金额(最多50个),并从初始余额中减去它们。然后,如果存在利息值,则将其乘以总数。结束结果使用balanceAccount()函数计算。我相信一切正常,直到最后的计算。我一直得到不正确的计算。我最终将包含一种货币格式,但现在我只是想让它正确计算。谢谢你的时间。
答案 0 :(得分:1)
double check[CHECK_MAX];
account = (start - check[CHECK_MAX]) * (1 + interest);
访问check[CHECK_MAX]
无效 - 它正在运行数组的末尾。 C ++数组的索引为0-n-1,因此数组中的最后一项是check[CHECK_MAX-1]
。
答案 1 :(得分:0)
balanceAccount()
中的所有变量都被初始化为零,因此main()
中的其他变量无效。要解决此问题,请将变量传递给balanceAccount()
:
void balanceAccount(double localAccount, double localStart, double localInterest, double localCheck[])
{
account = (start - check[CHECK_MAX]) * (1 + interest);
cout << account;
}
您可能还想返回account
。&#39;
这一行:
account = (start - check[CHECK_MAX]) * (1 + interest);
超过数组的末尾,它应该是:
account = (start - check[CHECK_MAX-1]) * (1 + interest);
我也不知道你在if...else
结束时对main()
所做的事情。
答案 2 :(得分:0)
将balanceAccount()方法改为如下..
double balanceAccount(char account[])
{
double start = 0;
double interest = 0;
double check[CHECK_MAX];
double account = (start - check[CHECK_MAX-1]) * (1 + interest);
cout << account;
}
主要balanceAccount()
total = balanceAccount(check);
在check
中传递balanceAccount()
的原因;您在check
和main()
中创建了不同的balanceAccount()
,两者都不相同...... < / p>
同样check[CHECK_MAX]
操作无效... 0 to CHECK_MAX-1
是有效的内存位置,根据上面的代码更改为CHECK_MAX
...更改
答案 3 :(得分:0)
基于您的陈述的工作代码试试这个,
#include<iostream>
using namespace std;
const int CHECK_MAX = 50;
void balanceAccount(double total,double interest)
{
double account = (total) * (interest);
cout << account;
}
int main()
{
int sub;
double start = 0;
double interest = 0;
double total = 0;
double check[CHECK_MAX];
void balanceAccount(double,double);
cout << "Enter the beginning balance of the account at the beginning of the month." << endl;
cin >> start;
total = start;
cout << "Enter the individual dollar amount of checks written this month." << endl;
cout << "A maximum number of 50 checks is enforced." << endl;
cout << "Enter a zero when you are finished entering check values." << endl;
for (sub = 0; sub < CHECK_MAX; sub++)
{
cout << "Enter the dollar amount for the checks written, but one at a time." << endl;
cin >> check[sub];
total = total - check[sub];
if (check[sub] == 0)
{
cout << "Thank you." << endl;
break;
}
}
cout << "If applicable, enter an interest rate." << endl;
cout << "If there is no interest rate, enter a 0." << endl;
cin >> interest;
if(interest != 0)
{
cout << "Thank you." << endl;
balanceAccount(total,interest);
}
else
{
cout << "Thank you." << endl;
cout << total << endl;
}
return 0;
}