Skip to main content
gamedev.net
🔒 Locked

What's wrong with my code?

Started by Gary the Llama 4 replies 300+ views
Original Post
Gary the Llama
Gary the Llama
I'm writing a little program to figure out how much money you'll have after x years with x amount of interest. It's not working right though, I think my for loop is screwed up, it's supposed to run the code inside the loop once for each year (so if it's for four years, the loops is supposed to run four times.) For some reason though, it always gives me the answer for only one year of interest and not four. Can someone take a loop at my crappy code and tell me what I did wrong. Thanks!
  
#include <stdio>
using namespace std;

int main()
{
   float initamount,
         interest,
         total;
   int numyears;

   cout << "What is the initial amount? ";
   cin >> initamount;
   cout << "How many years? ";
   cin >> numyears;
   cout << "What is the interest per year? ";
   cin >> interest;

   for (int j = 1; j >= numyears; j++)
   {
      total = initamount + (initamount * interest / 100);
   }
   cout << "The total after " << numyears << " is "
        << total << '\n';

   return 0;
}
   
Gary the Llama [edited by - Gary the Llama on June 5, 2002 3:12:38 PM]
DoggettCK
DoggettCK
Well, the first problem is in your for loop. You have:

  for (int j = 1; j >= numyears; j++)   {      total = initamount + (initamount * interest / 100);   }  


It should be j <= numyears, instead of >=, plus, it really should go from 0 to numyears-1, so try this:


  for (int j = 0; j < numyears; ++j)   {      total = initamount + (initamount * interest / 100);   }  
DoggettCK
DoggettCK
Try this:


  
#include <iostream>
using namespace std;

int main(void)
{
float initamount = 0.0f, interest = 0.0f, total = 0.0f;
int numyears = 0;

cout << "What is the initial amount? ";
cin >> initamount;
cout << "How many years? ";
cin >> numyears;
cout << "What is the interest per year? ";
cin >> interest;

total = initamount;

for (int j = 0; j < numyears; ++j)
{
total += total * (interest/100.0f);
}

cout << "The total after " << numyears << " is " << total << ''\n'';

return 0;
}
Gary the Llama
Gary the Llama
anon poster:

yeah, i figured it was a math problem but i was just brain farting today. thanks for the help!
DoggettCK
DoggettCK
quote:
Original post by Anonymous Poster
the last guy is right...kinda
it should be total= total+(initintrest*intrest/100)



If I understood the problem correctly, wouldn''t the interest also cover the interest earned from the previous year?

e.g. Start with an intial amount of 100, interest rate of 25, over 4 years.

Year 0 (start) amount: 100
Year 1 amount: 100 * 1.25 = 125
Year 2 amount: 125 * 1.25 = 156.25
Year 3 amount: 156.25 * 1.25 = 195.3125
Year 4 amount: 195.3125 * 1.25 = 244.140625

That''s the way my bank does it, at least, though I wish I could get a 25% interest rate.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.