I would use a more expressive name instead of 'x', something like "currentYear" :S
Also you don't need to declere 'i' outside the loop and especially not so far away from it.
Wouldn't this produce the same output?
1 2
|
for (int currentYear = 1; currentYear <= years; currentYear++)
cout << "Value of investment is: $" << compoundAnnual(startValue, interestRate, currentYear ) << endl;
|
So you get to remove 2 variables from the top.
Also, notice your function takes a double for year, yet your x is an int, I would change one of the two to match and avoid debugging future truncation problems :S
also this:
1 2 3 4
|
double compoundAnnual(double startValue, double interestRate, double years)
{
return pow((startValue*(1 + interestRate / 100)), years);
}
|
you don't need to store 'principal ' inside a function when you can just return it (since you're not doing anything else with it), I think it resoult in less variables construction though I can't be sure of it.
For the function checking input validity, watch on <cctype> header, also you want to check the cin methods called peek(),ignore(),fail(),clear()