0

My code compiles nicely, but the math formulas that I am using aren't providing the right outcome. I need to calculate the balance, withdrawn, and interest for all 3 months. I am also required to validate user's input. For these purposes I am using nested loops. Please let me know if you spot my mistake. Thank you lovely people!

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;
  • 1
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and desired behavior. What is your input? What is your desired output? – MikeCAT Mar 13 '16 at 04:27
  • 1
    Two things: 1. We need to know the declaration of your variables; 2. At the bottom, you are comparing 'withdrawn' and 'startBalance' (both of which I presume are float). You cannot compare floats directly in this way. Please refer: http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison for details. –  Mar 13 '16 at 04:31
  • 1) I have set all my variables to double, bool goodChoice = false. 2) I am saying that the outcome is wrong bc after I plugged in numbers I got a negative results – sunnysmile24 Mar 13 '16 at 05:16
  • cout << "*** Choice must be positive or greater than the balance***" << endl; this statement contradicts with your condition. – Abdul Rehman Mar 13 '16 at 05:21
  • even though all of my input was positive, for total withdrawn I got -1.#QNAN – sunnysmile24 Mar 13 '16 at 05:30

1 Answers1

0

You have a lot of extra variables defined which aren't needed. Also, your interest rate may have been in percentage instead of a decimal number, i.e. 10% = 0.1. Also, your monthInterest is taking an average then applying interest. I wrote this up and it seems to work.

#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;
}
user2205930
  • 1,046
  • 12
  • 26