assigning a value to variables

Im trying to calculate the balance at the end of the year given an annual investment, and a percentage rate of return. to do this im just putting the equation into a new variable labelled endYearBalance. when i try to use endYearBalance in the cout it gives me the wrong answer but when i take just the equation and stick it in, it gives me the right answer.... any help? the reason im trying to create a new variable for the equation is because this program should calculate and display the amount of years it will take to reach your ending investment goal...any help is appreciated

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
 void investmentInput(double& annualInvestment, double& returnRate, double& endGoal)

{
int year = 1;
double endYearBalance;
endYearBalance = (annualInvestment * (returnRate/100)) + annualInvestment;
double newYear;
newYear = endYearBalance + annualInvestment;


cout << " Enter the amount you want to invest annually: ";
cin >> annualInvestment;

cout << " Enter the yearly percentage rate of return: ";
cin >> returnRate;

cout << " Enter investment goal: ";
cin >> endGoal;

cout << "Year" << "     Balance (Jan. 1)" << "     Balance (Dec. 31)" << endl;
cout << year << "        "<< annualInvestment << "                  " << endYearBalance << endl; 
// this gives me the wrong answer;
// but when i input (annualInvestment * (returnRate/100)) + annualInvestment
// on its own i get the right answer... 

}
Last edited on
You are calculating the endYearBalance on line 6 before you've even read the values the user input. So it's working with whatever data you pass, not what user inputs.
OMG OFCOURSE!! thank you!!!
Topic archived. No new replies allowed.