#include <iostream>
 #include <cmath>


using namespace std;


 int main ()

 {  
       int numMonths;
       double balance;
        double numBalance;
       float rate;
       double totDeposited = 0.0 ;
       double totWithdrawn = 0.0 ;
       double mthlyInterRate = 0.0;
      double totInterest = 0.0;

     cout << " Enter the initial balance ===> ";
     cin >> balance;
     cout << " Enter the number of months to cover: ===> ";
     cin >> numMonths;
     cout << " Enter the annual interest rate (decimal  format; e.g., 5 not .05) ===> ";
     cin >> rate;

     for (int month = 1; month <= numMonths; month++)
    { double deposited, withdrawn;

     cout << " Enter the amount deposited for month  ===> " << month <<":" ;
     cin >> deposited;
     cout << " Enter the amount withdrawn for month  ===>  "<< month <<":" ;
     cin >> withdrawn;

     mthlyInterRate = (rate/100)/12;
     totDeposited += deposited ;
     totWithdrawn += withdrawn ;
     numBalance = balance + deposited - withdrawn;
     mthlyInterRate = numBalance *mthlyInterRate;
     totInterest += mthlyInterRate;
     numBalance += mthlyInterRate;
     }




      cout << " Starting balance:                   $ " << balance << endl;       
      cout << " Total amount deposited:       $ " << totDeposited << endl;
      cout << " Total amount withdrawn:     $ "  << totWithdrawn << endl;
      cout << " Total interest earned:           $ " << totInterest << endl;
      cout << " Final Balance:                      $"  << numBalance << endl;




     return 0;

     }

this is how it suppose to come out plzzz help me

Enter the initial balance ===> 1000
Enter the number of months to cover: ===>  3
Enter the annual interest rate (decimal  format; e.g., 5 not .05) ===> 10
Enter the amount deposited for month 1 ===> 500
Enter the amount withdrawn for month 1 ===> 400
Enter the amount deposited for month 2 ===> 600
Enter the amount withdrawn for month 2 ===> 700
Enter the amount deposited for month 3 ===> 800
Enter the amount withdrawn for month 3 ===> 900

Starting balance:                   $       1000.00
Total amount deposited:       $       1900.00
Total amount withdrawn:     $       2000.00
Total interest earned:           $           25.64
Final balance:                      $         925.64

Recommended Answers

All 18 Replies

Please use code tags. What is the problem you are having? Please give greater detail. Thanks :)

#include <iostream>
 #include <cmath>

using namespace std;


 int main ()

 {  
	   int numMonths;
       double balance;
		double numBalance;
       float rate;
       double totDeposited = 0.0 ;
       double totWithdrawn = 0.0 ;
	   double mthlyInterRate = 0.0;
	  double totInterest = 0.0;

	 cout << " Enter the initial balance ===> ";
	 cin >> balance;
     cout << " Enter the number of months to cover ===> ";
	 cin >> numMonths;
     cout << " Enter the annual interest rate (decimal  format; e.g., 5 not .05) ===> ";
	 cin >> rate;

	 for (int month = 1; month <= numMonths; month++)
	{ double deposited, withdrawn;

     cout << " Enter the amount deposited for month  ===> " << month <<":" ;
	 cin >> deposited;
     cout << " Enter the amount withdrawn for month  ===>  "<< month <<":" ;
	 cin >> withdrawn;

	 mthlyInterRate = (rate/100)/12;
	 totDeposited += deposited ;
	 totWithdrawn += withdrawn ;
	 numBalance = balance + deposited - withdrawn;
	 mthlyInterRate = numBalance *mthlyInterRate;
	 totInterest += mthlyInterRate;
	 numBalance += mthlyInterRate;
	 }




      cout << " Starting balance:                   $       " << balance << endl;       
      cout << " Total amount deposited:             $       " << totDeposited << endl;
      cout << " Total amount withdrawn:            $       "  << totWithdrawn << endl;
	  cout << " Total interest earned:              $           " << totInterest << endl
	  cout << " Final Balance:                      $         "  << numBalance << endl;




	 return 0;

     }

having trouble calculating the total interest i cant get the right amount

suppose to come out like this

Enter the initial balance ===> 1000
Enter the number of months to cover: ===> 3
Enter the annual interest rate (decimal format; e.g., 5 not .05) ===> 10
Enter the amount deposited for month 1 ===> 500
Enter the amount withdrawn for month 1 ===> 400
Enter the amount deposited for month 2 ===> 600
Enter the amount withdrawn for month 2 ===> 700
Enter the amount deposited for month 3 ===> 800
Enter the amount withdrawn for month 3 ===> 900

Starting balance: $ 1000.00
Total amount deposited: $ 1900.00
Total amount withdrawn: $ 2000.00
Total interest earned: $ 25.64// cant get it to come out this way
Final balance: $ 925.64

OK. Your code now has a lots of random whitespace (tabs). Please edit your posts if the code looks like this. So the problem is that the interest doesn't calculate properly? Could you do one with much smaller numbers? It's easier to work with. I'm still confused as to what is wrong. So you want to calculate the amount of interest based on how many months it is in the bank for? Could you do a reworded demo with smaller numbers (much easier to grasp!), and give a deeper explanation.

#include <iostream>
 #include <cmath>
 using namespace std;
 
 int main ()
 
 {  
int numMonths;
double balance;	
double numBalance;
float rate;
double totDeposited = 0.0 ;
double totWithdrawn = 0.0 ;
double mthlyInterRate = 0.0;
double totInterest = 0.0;
 
cout << " Enter the initial balance ===> ";
cin >> balance;
cout << " Enter the number of months to cover ===> ";
cin >> numMonths;
cout << " Enter the annual interest rate (decimal  format; e.g., 5 not .05) ===> ";
cin >> rate;
 
	 for (int month = 1; month <= numMonths; month++)
	{ double deposited, withdrawn;
 
cout << " Enter the amount deposited for month  ===> " << month <<":" ;
cin >> deposited;
cout << " Enter the amount withdrawn for month  ===>  "<< month <<":" ;
cin >> withdrawn;
 
mthlyInterRate = (rate/100)/12;
totDeposited += deposited ;
totWithdrawn += withdrawn ;
numBalance = balance + deposited - withdrawn;
mthlyInterRate = numBalance *mthlyInterRate;
totInterest += mthlyInterRate;
numBalance += mthlyInterRate;
}
 

cout << " Starting balance:  $" << balance << endl;       
cout << " Total amount deposited:  $" << totDeposited << endl;
cout << " Total amount withdrawn:  $"  << totWithdrawn << endl;
cout << " Total interest earned:  $" << totInterest << endl;
cout << " Final Balance:    $ "  << numBalance << endl;
	 return 0;
 
     }

yes to calculate the amount of interest based on how many months it is in the bank for and smaller numbers work ok to i guess

to come out 25.64 but i dont know what i ave to do t make it come out like that

Great job with getting your post sorted! OK I've got it now! Sorry about that :/
I made a codez for you :)
It probably doesn't do exactly what you want it to do but check the logic between my code and yours.
I'd also recommend that you seperate up your main into 2 functions. Keep the user input in 1, and put the interest calculation logic in another, like my code below.

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

float getInterest(float deposit, float interest, int months)
{
    float monthInterest = (interest/12);
    float earnedInterest = (monthInterest*months)*deposit;
    return earnedInterest;
}

int main()
{
    float balance, deposit, interest;
    int months;
    cout<<"Enter balance (eg. £472.91):"<<endl;
    cin>>balance;
    cout<<"Enter deposit (eg. £22.01):"<<endl;
    cin>>deposit;
    cout<<"Enter annual interest rate (eg. 2.5%):"<<endl;
    cin>>interest;
    cout<<"Enter months until withdrawal (time to generate interest, int):"<<endl;
    cin>>months;
    cout<<"===================================="<<endl;
    
    float earnedInterest = getInterest(deposit, interest, months);
    cout<<"Balance             :           £"<<balance<<endl;
    cout<<"Deposit             :           £"<<deposit<<endl;
    cout<<"Interest            :           £"<<interest<<endl;
    cout<<"Months              :           £"<<months<<endl;
    cout<<"Earned interest     :           £"<<earnedInterest<<endl;
    cout<<"Total Balance       :           £"<<balance+earnedInterest<<endl;
    cout<<"=============================="<<endl;
    system("pause");
    
    return 0;
}

A couple more points with your code:

1. You don't need to set "0.0" as a value when you define a float/double.
eg.

double a = 0.0;
//is the same as
double a;

2. If you get rid of these, the top of your main function can become:

int numMonths;
float rate;
double balance, numBalance, totDeposited, totWithdrawn, mthlyInterRate, totInterest;

!!!EDIT!!! Sorry I forgot to put that you need to round the total balance to 2dp. I'm sure you can work that out though :)

i did everything to come out like this as u see in my code but the interest doesnt calculate right to the interest down there
its suppose to come out like this

Enter the initial balance ===> 1000
Enter the number of months to cover: ===> 3
Enter the annual interest rate (decimal format; e.g., 5 not .05) ===> 10
Enter the amount deposited for month 1 ===> 500
Enter the amount withdrawn for month 1 ===> 400
Enter the amount deposited for month 2 ===> 600
Enter the amount withdrawn for month 2 ===> 700
Enter the amount deposited for month 3 ===> 800
Enter the amount withdrawn for month 3 ===> 900

Starting balance: $ 1000.00
Total amount deposited:$ 1900.00
Total amount withdrawn:$ 2000.00
Total interest earned:$ 25.64// this is the amount i need to come out but cant get it
Final balance: $ 925.64

im srry for the confusion of everything here

Eeek...so the interest has to be calculated individually on each deposit?!

OK...that's where I was going wrong. What you would want to do is to feed the deposit from each month into a (double/float) array. Then, have a separate array of the same length detailing how long the corresponding deposit has been in the bank for (integer array). Then use a while/for loop to work through and calculate the interest for each deposit into another array. Then add up the totals at the end.

Are you sure that you need to get that amount of interest? The reason I asked you to use simple numbers is that it is no longer impossible to work out the true value, and will make it easier to determine errors in your code/logic.

yes im sure i need to get the amount of interest earned im very new to all this so sooooorrrrryyy for everthing

ahh it still wont come out right

OK. Sorry I can't write out much more code for you 'cos I've got a 3D modeling project to sort out. Here are some links to get you help on this.
Arrays
Time Value Of Money (Just take a glance)

Please can you upvote my posts seeing as I have helped you out :)

how do i do that

oh and thx for helping me

Thanks :) I shall be back later

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.