I just started learning C++ a week ago and im trying to get a counting loop to work but I cant. Im going nuts because I know somewhere in my code there is a syntax error but I cant seem to be aböe to figure it out! Maybe I stare at it tool long =). If someone could help me, I would be much obliged!
#include <iostream>
#include <string>
#include <cmath>
usingnamespace std;
int main ()
{
cout << " -------------------------------------" << endl; // There is nothing wrong with a little bit
cout << " | Welcome to Bank of Coders! |" << endl; // of BRANDING!
cout << " -------------------------------------" << endl;
cout << " " << endl;
/////////////////////////////////////////////////////////////
// Here are trying to get some information from the customer
/////////////////////////////////////////////////////////////
float deposit;
cout << " | Enter deposit amount!: ";
cin >> deposit;
float goal;
cout << " | How much are you looking to save?: ";
cin >> goal;
float rate;
cout << " | Enter your rate!: ";
cin >> rate;
float balance=deposit;
int years;
/////////////////////////////////////////////////////////////
// This is the formula for calculating compound interest,
// deposits and the end balance compared to the customers goal
/////////////////////////////////////////////////////////////
while (balance < goal)
{
balance = balance + balance*(rate/100)+deposit;
balance++;
}
cout << " -------------------------------------" << endl;
cout << " | Your balance will be: " << balance << endl;
///////////////////////////////////////
// This code will count how many years
// it will take to save to my goal
///////////////////////////////////////
for(years=0;years<goal;years++)
{
cout << " It will take you " <<years+1<< " to reach your goal"<<endl;
}
}
@SmallButPowerful
What? There is no need to do that at all... in fact, there aren't actually any syntax errors in @rookiecoder's code. There are, however, logic errors (presumably).
@OP
Could you please give us an example of an input and ouput that you expect? It would also help if you could tell us what problem you are trying to solve.
I tried putting semi ";" where you suggested but it does not make a difference. It keeps counting indefinitely.
Basically what I am trying to accomplish is, for the program to tell the user how many years it will take the user to save up to the entered goal amount.
Basically the program is designed to calculate how many years it will take for the user to save to his savings goal with interest, and then tell the user how many years it will take to reach his goal.
Example output:
-----------------------------------
Enter deposit amount!:
How much are you trying to save?:
Enter your rate!:
You balance will be : (balance+interest)
It will take you (years) to reach your goal!
You are having years<goal as the for loop condition. Don't forget that "goal" is the amount of money that needs to be reached. While "years" is the time taken.
Compound interest formula is: A = P(1+r/100)^t
Lets substitute the variables:
A = goal.
P = deposit.
r = rate.
t = years.
We need to find t. Therefore make t the subject of the formula. After doing the math, you get:
t = log[1+r/100] (A/P)
t = in(A/P)/in(1+r/100)
(in is the natural logarithm)
So you don't need a for-loop to calculate the years.
Here is a code representation of what I'm talking about:
1 2
years = static_cast<int>(log(goal/deposit*1.0)/log(1+rate/100.0));
cout << "It'll take " << years << " years to reach your goal.";
If you declare "years" as float or double, you don't need to cast it to int.
If you have trouble understanding anything here, feel free to ask. :)
I tried your code and it says that it will take 68 years to save to 50000 with a annual deposit of 3000 and a rate of 4.2! It should take max 13 years!
13 years? Lol, that's too fast. With a rate of 4.2% p.a, to make 3000 to 50000, it'd take more than 13 years.
3000(1+4.2/100)^13 = 5000.
3000(1+4.2/100)^63 = 50000.
Either you got your estimations wrong or you don't know the compound interest formula.
Maybe I my initial introduction was not clear enough, I apologize!
The deposit is annual, in other words the user is depositing 3000 every year. So with a goal of 50000/3000=16.6 years excluding interest. So my formula for compound interest is not wrong. My loop for counting how many years it takes is wrong.
cout << " -------------------------------------" << endl; // There is nothing wrong with a little bit
cout << " | Welcome to Bank of Coders! |" << endl; // of BRANDING!
cout << " -------------------------------------" << endl;
cout << " " << endl;
You can just do
1 2 3 4
cout << " -------------------------------------\n" // There is nothing wrong with a little bit
cout << " | Welcome to Bank of Coders! |\n" // of BRANDING!
cout << " -------------------------------------\n"
cout << "\n" << endl; // \n will print a new line and endl will flush the buffer and print a newline