Cant get my loop to work!!

Hi guys,

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <cmath>
using namespace 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;
        }
}
Last edited on
after the loop, you need to put a simi colon,

it would be:
1
2
3
4
5
while (balance < goal)
                {
                balance = balance + balance*(rate/100)+deposit;
                balance++;
                };


and:
1
2
3
4
for(years=0;years<goal;years++)
        {
            cout << " It will take you so many " <<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.
Hi SBP,

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.
Hi NT3,

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!
closed account (iAk3T05o)
@smallbutpowerful: your code is not the solution. You don't put ';' after any loop. You do that for a class or enum not a loop.

You need to lay out the formula for calculating compound interest first because i feel there's something wrong with the while and/or for loop.
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. :)

Cheers!
@>S>tormboy!

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!

I do not know how to fix the error!
I think that you shoud change
1
2
3
4
for(years=0;years<goal;years++)
        {
            cout << " It will take you " <<years+1<< " to reach your goal"<<endl;
        }

to:
1
2
for(years=0;years<goal;years++){}
cout << " It will take you " <<years+1<< " to reach your goal"<<endl;
@Btoven

It does not work! It tells me it takes 5001 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.
Last edited on
@Stormboy

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.
Also with the start:

1
2
3
4
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 
@ rookiecoder: Ah I see. Try this then:
1
2
3
4
5
6
7
8
9
10
float dummy = deposit;
int years = 0;
while(dummy <= goal)
{
    dummy += dummy(1+rate/100.0);
    dummy += deposit;
    years++;
}

cout << "It'll take " << years << " to reach your goal." << endl;


@ YellowPyrmid: Use of endl is recommended. It also makes your code look more clean.
Last edited on
Topic archived. No new replies allowed.