#### Archived

This topic is now archived and is closed to further replies.

# What's wrong with my code?

This topic is 5981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
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);   }

##### Share on other sites
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;}

##### Share on other sites
your problem is a simple math one.
Your just doing the same calc every time, w/ the same numbers
total=initialinterst*(blah blah blah)
the numbers in the loop never change(inital intest is allways what they set it too) so your answer never changes
the last guy is right...kinda
it should be total= total+(initintrest*intrest/100)

##### Share on other sites
anon poster:

yeah, i figured it was a math problem but i was just brain farting today. thanks for the help!

##### Share on other sites
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.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 73
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632967
• Total Posts
3009578
• ### Who's Online (See full list)

There are no registered users currently online

×