【问题标题】:Summing up the values of a variable after a loop has ended循环结束后对变量的值求和
【发布时间】:2014-11-18 17:40:52
【问题描述】:

你好,这是我的程序的目的:

  1. 储蓄账户余额

编写一个程序,计算三个月期末储蓄账户的余额。它应该向用户询问起始余额和年利率。然后,循环应该在此期间每个月迭代一次,执行以下步骤:

A) 询问用户当月存入账户的总金额,并将其添加到 平衡。不接受负数。

B) 询问用户当月从账户中提取的总金额并减去 从余额。不接受负数或大于余额后的数字 当月的存款已经加入。

C) 计算当月的利息。月利率是年利率 除以 12。将月利率乘以该月开始的平均值 和期末余额以获得当月的利息金额。应添加此金额 到天平。

在最后一次迭代之后,程序应该显示一个包含以下内容的报告 资料:

  • 三个月期初的期初余额
  • 三个月内的总存款
  • 三个月内的总提款
  • 三个月内记入帐户的总利息
  • 最终余额

我遇到的问题是,最后当我显示表格时,我的总存款、取款和利息金额只显示循环结束时的最后一个实例,而不是三个月的总和。这是我的代码,如果它不必要的复杂或混乱,请见谅。

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//variables
double depositAmount;
double withdrawAmount;
int monthPeriod = 3;
double startBalance;
double finalBalance;
double totalBalance;
double annInterestRate;
double monthInterestRate;
double monthInterestAmount;
double monthAverageBalance;
double monthAverageAmount;
int count;

cout << "What is your starting balance?   ";
cin >> totalBalance;
cout << "What is your annual rate?  "; 
cin >> annInterestRate;

for (count = 1; count <= monthPeriod; count++)
{
    cout << "Enter total amount deposited for the month  ";
    cin >> depositAmount;
    while (depositAmount < 0)
    {
        cout << "Error, no negative amounts, please try again." << endl;
        cin >> depositAmount;
    }
    cout << "Enter total amount withdrawn for the month  ";
    cin >> withdrawAmount;
    while (withdrawAmount < 0 || withdrawAmount > totalBalance)
    {
        cout << "Error, no negative amounts or withdrawals greater than your balance. Please try    again" << endl;
        cin >> withdrawAmount;
    }
    startBalance = totalBalance + depositAmount;
    finalBalance = totalBalance - withdrawAmount;
    totalBalance = startBalance - finalBalance;
    monthInterestRate = annInterestRate * 12;
    monthAverageBalance = (startBalance + finalBalance) / 2;
    monthInterestAmount = monthAverageBalance * monthInterestRate;
    totalBalance = monthInterestAmount + totalBalance;
}
cout << "Your starting balance at the beginning of three months " << startBalance << endl;
cout << "Total deposits over three months  " << depositAmount << endl;
cout << "Total withdrawals over three months  " << withdrawAmount << endl;
cout << "Total interest posted to account over three months " << monthInterestAmount << endl;
cout << "Final Balance: " << totalBalance << endl;
cout << "Thank you for using the program!" << endl;
return 0;
}

【问题讨论】:

  • 重新考虑使用变量的方式(按名称)。例如,不应该将 cout &lt;&lt; "What is your starting balance? "; cin &gt;&gt; totalBalance; 设置为读入 startBalance 吗?在纸上写下你将如何做到这一点。你写下的第一行是startBalance,对吧?那么您将如何处理与该起始余额相关的存款 (+) 或取款 (-)?
  • 你是对的,我会改变它。虽然我的逻辑是一开始我有一个起始余额,然后我添加或减去存款/取款。因此,totalBalance。
  • 不,totalBalance 将是您最终得到的结果,当您产生 startBalance + deposits - withdrawals 的最终结果时。

标签: c++ mysql sql-server loops


【解决方案1】:

这似乎是一个硬件问题,所以也许我会给出一个提示。提示:每月存款累积的变量是什么?

另外,一个不请自来的答案:您的月利率可能不是 12*Annual rate,它可能是年利率 / 12。希望这会有所帮助。

【讨论】:

  • 我在想我可能需要一个变量来累积每月的总和,我只是不明白如何编码它来收集三个不同的金额。
  • += 是你的朋友。类似 Total_Deposit += depositAmount(相当于说 Total_Deposit = Total_Deposit + depositAmount)。当我第一次开始编码时,= 的使用让我发疯了,因为从数学上讲,这两个方面是不相等的。这是一个赋值语句:使 Total_Deposit 的新值等于 Total_Deposit 的旧值 + depositAmount。
  • 哦!我完全忘记了 +=,这正是我所需要的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 2020-12-31
相关资源
最近更新 更多