What am I doing wrong?
***********************************************************
#include <iostream>
using namespace std;
int main()
{
float years = 1;
float goal = 50000;
float deposit = 3000;
float interest = ((years * deposit) * (4.2 / 100));
float balance = ((years * deposit) + interest);
while (goal > balance)
{
years++;
}
cout << "Your goal is reached after " << years << " years." << endl;
cout << "Your balance will be " << balance << " dollars.";
}
***************************************************************
I should end up with 13 years and 50513.15 dollars.
>>41530
>***********************************************************
>#include <iostream>
>using namespace std;
>int main()
>{
>float years = 0;
>float goal = 50000;
>float deposit = 3000;
>float interest, balance;
>while (goal > balance)
>{
>years++;
>interest = ((years * deposit) * (4.2 / 100));
>balance = ((years * deposit) + interest);
>}
>cout << "Your goal is reached after " << years << " years." << endl;
>cout << "Your balance will be " << balance << " dollars.";
>}
>***************************************************************
>I should end up with 13 years and 50513.15 dollars.
Statements like "interest = ((years * deposit) * (4.2 / 100));" don't create a function that tell the compiler how to calculate the Left-Hand Side every time it's needed, they tell the compiler to evaluate the RHS once, and store that value in the LHS when that line is executed.
So if you want to do the calculation every time in the loop, you need the statement to be inside the loop body.
Also you're calculating compound interest wrong, but you asked about your code, not your algorithm.
>>41539
Thank you so much!!
Though, what's wrong with the algorithm?
>>41530
This would be how you calculate the interest correctly.
>===================
#include <iostream>
using namespace std;
int main()
{
int years = 0;
float balance = 0;
const float goal = 50000; // the program's not supposed to change these values, so mark them const
const float deposit = 3000; // this means that even a dumb compiler will inline them, saving memory
const float interest_rate = 1 + (4.2/100); // and will warn if we change them when we're not supposed to
while (balance <= goal) // constants on LHS is safer, sure, but RHS is congruent to English's "While the <whatever> is <whatever>" syntax
{
years += 1; //++ for when you're using the value in an expression, += for when you're not
balance += deposit;
balance *= interest_rate;
//cout << "It's year " << years << ", and you have a balance of" << balance << " dollars."<< endl;
}
cout << "Your goal is reached after " << years << " years." << endl;
cout << "Your balance will be " << balance << " dollars.";
}
>>41542
interest = ((years * deposit) * (4.2 / 100));
balance = ((years * deposit) + interest);
so balance = ((years * deposit) + (years * deposit) * (4.2 / 100));
so balance = ((years * deposit) * (1+ (4.2 / 100)));
so balance = ((years * deposit) * (1.042));
This isn't correct, because it's only applying the interest for each yearly deposit once, on the year it's deposited. With compound interest, each year's deposit gets interest added every year, not just once. So on year five:
year 5 is compounded 1 time
year 4 is compounded 2 times
year 3 is compounded 3 times
year 2 is compounded 4 times
year 1 is compounded 5 times
And so on.