I've been struggling with this assignment for some time, but I feel as though I've finally got a hold on it. Problem is, I got it to work ONCE, then changed one tiny thing and broke it.
Okay, so the assignment was to write a program that computes how much an investment would be worth in a few years. Lots of cin for the various values and a formula that was a little bit annoying to figure out. Simple stuff, so you're probably all giggling at how I screwed it up. Laugh it up, fuzzball.
So here's the formula:
amount = principal(1 + rate / n) (n [A power. Dunno how to raise it in the post])(t [power again])
principal is the amount invested
rate is the decimal percentage rate
n is the number of times to compound per year
t is time in years
This is in math-speak, not attempted code. I'm not that dumb yet. So here's the sample run on the assignment paper:
Rate as a %: 6
Number of years: 10
Number of times to compound: 4
If $5000 had been invested for 10 years at 6%, the investment would now be worth $9070.09.
I feel the need to say this again: I got this to work. I put all the values in my code before trying the cin version, and it got it right.
Not so anymore. So here's the code I've got:
//Program to compute the value of and investment
using namespace std;
double principal, amount, rate, n, t, percent;
cout << "Enter: " << endl;
cout << "Principal: ";
cin >> principal;
cout << "Rate as a %: ";
cin >> percent;
rate = percent / 100.0;
cout << "Number of years: ";
cin >> t;
cout << "Number to compound: ";
cin >> n;
amount = principal * pow(1 + rate / n, (n * t));
cout << "If " << principal << " had been invested for " << t << " years at " << rate << " %, the investment /n would now be worth " <<
amount << "." << endl;