gpt4 book ai didi

c++ - 循环结束后对变量的值求和

转载 作者:行者123 更新时间:2023-11-29 23:25:36 25 4
gpt4 key购买 nike

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

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

    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;
    }

    最佳答案

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

    另外,一个主动回答:您的月利率可能不是 12*年利率,它可能是年利率/12。希望这会有所帮助。

    关于c++ - 循环结束后对变量的值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000544/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com