I get this as my ouput without adding in any values:
Output:
Enter deposit amount: Another (y or n)?
Total deposits: 0
Number of deposits: 1
Average deposit: 0
You want to invest in a new account that automatically deposits this average
amount once each year. The money earns interest, compounded annually, and
you want to know how much interest will be earned and the final balance.
Enter number of years to invest:
Enter annual interest rate (e.g. 8.5):
Number of years invested: 134541077
Interest Rate: 4.00027e-34
Amount Invested: 0
Interest Earned: 0
Final Balance: 0
Seems like there might be a problem with number of deposits?
Try changing everything to a double data type?
wow thats interesting. Had no idea. Wish I knew how to source it out. I spent so much time on this, I dont know where to look. Everything seem to make sense until the FOR loop.
Ive been working on this program for over 12 hours and I've been stuck on this compound interest part for over 4 hours. I made an account just to get some feedback on this. But im sure I will be on this forum a lot more.
Well here's the code.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
//Input Variables
char userResponse;
float depositAmount;
float totalDeposits;
int depositCount;
float averageDeposit;
// Accumulators
depositCount = 0;
totalDeposits = 0;
do
{
cout << "Enter deposit amount: ";
cin >> depositAmount;
totalDeposits =+ depositAmount + totalDeposits;
depositCount++;
cout << "Another (y or n)? ";
cin >> userResponse;
}while (userResponse == 'Y' || userResponse == 'y' );
//calculations
averageDeposit = totalDeposits / depositCount;
//output
cout << endl;
cout << "Total deposits: " << totalDeposits << endl;
cout << "Number of deposits: " << depositCount << endl;
cout << "Average deposit: " << averageDeposit << endl << endl;
//PART 2
// Message about investing in new interest bearing acount.
cout << "You want to invest in a new account that automatically deposits this average" << endl;
cout << "amount once each year. The money earns interest, compounded annually, and" << endl;
cout << "you want to know how much interest will be earned and the final balance." << endl;
// Declare Variables
float interestRate;
int yearsToInvest;
float annualDeposit;
float balance;
float amountInvested;
float interestEarned;
// Accumulator / Pre-calculations
balance = 0;
annualDeposit = averageDeposit;
cout << endl;
cout << "Enter number of years to invest: ";
cin >> yearsToInvest;
cout << endl;
cout << "Enter annual interest rate (e.g. 8.5): ";
cin >> interestRate;
cout << endl;
for (int yearCounter = 1; yearCounter <= yearsToInvest; yearCounter++);
{
balance += annualDeposit * yearsToInvest;
balance = balance * pow(1 + interestRate/100, yearsToInvest) ;
}
//Calculations
amountInvested = annualDeposit * yearsToInvest;
interestEarned = balance - amountInvested;
//Output
cout << endl;
cout << "Number of years invested: " << yearsToInvest << endl;
cout << "Interest Rate: " << interestRate << endl;
cout << "Amount Invested: " << amountInvested << endl;
cout << "Interest Earned: " << interestEarned << endl;
cout << "Final Balance: " << balance << endl;
return 0;
}
The program runs fine when I'm using only '1' for number of years invested and deposits.. However, when I use 2 or more years for number of years invested, the program doesn't do the math correctly.
Enter deposit amount: 500
Another (y or n)? n
Total deposits: 500
Number of deposits: 1
Average deposit: 500
You want to invest in a new account that automatically deposits this average
amount once each year. The money earns interest, compounded annually, and
you want to know how much interest will be earned and the final balance.
Enter number of years to invest: 2
Enter annual interest rate (e.g. 8.5): 8.5
Number of years invested: 2
Interest Rate: 8.5
Amount Invested: 1000
Interest Earned: 177.225
Final Balance: 1177.23
Press any key to continue . . .