0

私のコードは問題なくコンパイルできますが、使用している数式では正しい結果が得られません。3 か月すべての残高、引き出し、および利息を計算する必要があります。また、ユーザーの入力を検証する必要があります。これらの目的のために、ネストされたループを使用しています。私の間違いを見つけたら教えてください。ありがとう、素敵な人たち!

cout << "Please enter the starting balance: ";
cin  >>  startBalance;  
cout << "Please enter the annual interest rate: ";
cin  >> annualInterest;

for (int month = 1; month <= 3; month++) {

       do { 
        cout << setprecision(5);
        cout << "Please enter the total amount deposited on month " << month << ": ";
        cin >> balance; 

        if (balance <0)  {  
            goodChoice = false;
            cout << "\n\t***ERROR " << balance << " ***";
            cout << "*** Choice must be positive***\n" << endl;
            }

            else {
             goodChoice = true;
            }

        startBalance += balance; //need to calculate the balance for all 3 months

           } while (!goodChoice); 

       do { 
        cout << setprecision(5);
        cout << "Please enter the total amount withdrawn on " << month << ": ";
        cin >> withdrawn; 

            if ( (withdrawn <0) || (withdrawn > startBalance) ) {  
            goodChoice = false;
            cout << "***ERROR " << withdrawn << " ***";
            cout << "*** Choice must be positive or greater than the balance***" << endl;
            }

            else {
             goodChoice = true;
            }
             finalWithdrawn += withdrawn; // the total amount of withdrawn

             finalBalance = startBalance - withdrawn;

          monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
          totalInterest += monthInterest; //total interest for all 3 months
          endBalance = monthInterest + finalBalance;


        } while (!goodChoice);  
}

cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
4

1 に答える 1

0

必要のない余分な変数がたくさん定義されています。また、利率が小数ではなくパーセンテージ (10% = 0.1) になっている場合があります。また、あなたmonthInterestは平均を取ってから利息を適用しています。私はこれを書きました、そしてそれはうまくいくようです。

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float totalDeposit = 0.0f;
    float totalWithdrawn = 0.0f;
    float totalInterest = 0.0f;
    float totalBalance = 0.0f;

    float interestRate = 0.0f;

    setprecision(5); //?

    cout << "Enter starting balance: ";
    cin >> totalBalance;
    cout << "Enter annual interest rate (%): ";
    cin >> interestRate;

    // Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
    interestRate = interestRate / 100.0f / 12.0f;

    for (int month = 1; month <= 3; month++)
    {
        float deposit = -1.0; // Default to an error state

        while (deposit < 0.0)
        {
            cout << "Enter total deposited in month " << month << ": ";
            cin >> deposit;

            if (deposit < 0.0f)
            {
                cout << "ERROR: Invalid amount" << endl;
                continue;
            }
        }

        float withdrawn = -1.0f; // Default to an error state

        while (withdrawn < 0.0f)
        {
            cout << "Enter total withdrawn in month " << month << ": ";
            cin >> withdrawn;

            if (withdrawn < 0.0f)
            {
                cout << "ERROR: Invalid amount" << endl;
                continue;
            }
        }

        totalDeposit += deposit;
        totalWithdrawn += withdrawn;
        totalBalance = totalBalance + deposit - withdrawn;
        totalBalance += totalBalance * interestRate;
        totalInterest += totalBalance * interestRate;
    }

    cout << "Total Deposit: " << totalDeposit << endl;
    cout << "Total Withdrawn: " << totalWithdrawn << endl;
    cout << "Total Interest: " << totalInterest << endl;
    cout << "Final Balance: " << totalBalance << endl;

    int wait; // Pause so console window doesn't close. Delete this line.
    cin >> wait;

    return 0;
}
于 2016-03-13T05:30:28.843 に答える