C++ track: interest is interesting

This is the code I wrote for calculating the compound interest but I am only getting passes in two tests. help me kindly


int years_until_desired_balance(double balance, double target_balance) 
{
    if ( balance > 0 && balance >= target_balance) return 0;
    else
    {
        double ab = balance;
        int i =1;
    if (balance<0) 
     {  if (balance < target_balance) return -1;
         if ( balance == target_balance) return 0;
        else
    {for ( i=1 ; ab >= target_balance ; i++) 
            { 
                double rate = (3.213/100);
               ab = ab * ( 1+rate ); 
            }
    return I;
    }}
      if (balance>=0 && balance < 1000) 
      { for ( int i=1 ; ab <= target_balance ; i++) 
            { 
                double rate = (0.5/100);
               ab = ab * (1+rate); 
            }return I;
            }
      if (balance>=1000&&balance <5000) 
      {for ( int i=1 ; ab <= target_balance ; i++) 
            { 
                double rate = (1.621/100);
               ab = ab *(1+rate); 
            }return I;
            }
    else 
      { for ( int i=1 ; ab <= target_balance ; i++) 
            { 
                double rate = (3.213/100);
               ab = ab * (1+rate); 
            } return I;
            }}
}

Is that the complete file? What is in the header? What do the tests show?

Please provide all the info :slight_smile:

1 Like

You can greatly simplify your code by reusing the annual_balance_update function.

But I think the main problem is you have the wrong default interest rate (which you could have prevented by reusing another of your functions).

Another thing to keep in mind: while you are looping and accumulating interest on the balance, the interest rate might change as the balance grows.

1 Like

ahh thank you makes sense