Can someone tell me what is wrong with my code? The instructions are included in the comments. What keeps happening is that the program loops correctly, but for each year the amount of money remains the same. Can anyone tell me what I'm doing wrong?
/*Use a flowchart or pseudocode to design and then to write the program that will determine the number of years it will take to accumulate
a specified sum of money. There is no need to loop for multiple sets of input.
Input is the amount of the initial investment, the yearly interest rate, and the amount desired. Prompt accordingly.
Output to the screen for each year:
The year number (ie: 1, 2, 3, …)
The beginning amount for that year
The ending amount for that year
Output the values in columns with 2 decimal places for the money fields. Design a print chart appropriately including headings.
Continue looping until the desired amount is reached or exceeded.*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>
usingnamespace std;
int main ()
{
int current, desired, startAmt, endAmt, year;
float percent, increase;
cout << "How much do you have now? ";
cin >> current;
cout << "How much do you need to have? ";
cin >> desired;
cout << "What is the percentage increase per year in decimals? ";
cin >> percent;
cout << endl;
startAmt = current;
increase = (current * percent);
endAmt = increase;
year = 0;
while (current < desired)
{
current = current + increase;
year++;
cout << "Start amount for year " << year << ": " << startAmt << endl;
cout << "End amount for year " << year << ": " << endAmt << endl << endl;
}
return 0;
}
All you're doing is adding increase to current in your while loop.
I don't know why you'd expect startAmt and endAmt to change when you're not modifying them through the loop.
Edit: Also, the only include you're using is <iostream>. You should remove the others.
usingnamespace std;
int main()
{
int current, desired, startAmt, endAmt, year;
float percent, increase;
cout << "How much do you have now? ";
cin >> current;
cout << "How much do you need to have? ";
cin >> desired;
cout << "What is the percentage increase per year in decimals? ";
cin >> percent;
cout << endl;
year = 0;
while (current < desired)
{
increase = current * percent;
endAmt = current+increase;
current = current + increase;
startAmt = current;
year++;
cout << "Start amount for year " << year << ": " << startAmt << endl;
cout << "End amount for year " << year << ": " << endAmt << endl << endl;
}
}
The problem with yours is that the loop does its own thing with current and doesn't refer to the "startAmt= current" and the rest of the stuff you were supposed to put in the loop
I'm kind of new to c++ so my explanation is kind of awky lol
I tried both suggestions and neither of them worked. I have tried placing my variables in and outside of the loop. Putting them outside the loop has actually given me numbers, but inside the loop has given me zeros.
Is there something wrong with my math? Is it the order I am placing everything in?
The problem is that I cannot get the amount to increment after each year. So the loop just goes on forever with no change in the numbers. The year does however increase correctly after moving it like so:
cout << "Start amount for year " << year << ": " << startAmt << endl;
cout << "End amount for year " << year << ": " << endAmt << endl << endl;
year++;
}
Post your modified code.
It doesn't matter where you INITIALIZE the variables (as long as they're in the scope you're using them in), you just need to MODIFY them through the for loop...
If you're having trouble with something as minor as this, I highly recommend looking up a video on Youtube that goes over loops and variables, or going through the tutorial on this site.