在循环结束后总结变量的值

时间:2014-11-18 17:40:53

标签: c++ mysql sql-server loops

您好,这是我的计划的目的:

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

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

1 个答案:

答案 0 :(得分:0)

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

另外,一个未经请求的答案:您的月利率可能不是12 *年利率,可能是年率/ 12.希望这会有所帮助。