# Thread: Annuity/Bank interest program. HELP

1. ## Annuity/Bank interest program. HELP

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:

Enter:
Principal: 5000
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:

Code:
```//Program to compute the value of and investment

#include <iostream>
#include <cmath>
using namespace std;
void main()
{
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;
}```

2. The code as is gives 9070.09 whether you use cin or hard code the numbers into it. So is that the wrong answer? What were you expecting?

3. a power can be represented by a measly ^, you fuzzball

4. Originally Posted by Daved
The code as is gives 9070.09 whether you use cin or hard code the numbers into it. So is that the wrong answer? What were you expecting?
No, that's the correct answer. O_o So the code does fine? Well, perhaps my compiler is just finicky. I'll try it at the computer lab tomorrow.

I'm using the Visual C++ 2005 Express Edition Beta, by the way.

Originally Posted by durban
a power can be represented by a measly ^, you fuzzball
Well, I didn't know that, you scruffy nerd herder. Thanks, seriously.

5. >> So the code does fine?

In Visual C++ 2003 it does. I entered the values from the sample run and it gave me the same output. Other than void main (it should be int main), nothing about that code looks wrong.

6. Originally Posted by Daved
>> So the code does fine?

In Visual C++ 2003 it does. I entered the values form the sample run and it gave me the same output. Other than void main (it should be int main), nothing about that code looks wrong.
Eh, my prof got me into the habit of doing that. It's that way on all our lab assignments, so I'm kind of defaulted to it, sadly. Trying to stop.

Thanks a lot for the help. It's good to know that this actually works (at least for you!) and that it's practically ready to be handed in.

7. > a power can be represented by a measly ^, you fuzzball
Not so.
^ is the exclusive or operator (a bitwise operator for integers only).

8. Originally Posted by Salem
> a power can be represented by a measly ^, you fuzzball
Not so.
^ is the exclusive or operator (a bitwise operator for integers only).
I think he and I both meant on the forums, not in code. Just so ya know.

Popular pages Recent additions