Thread: Annuity/Bank interest program. HELP

  1. #1
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20

    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. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    a power can be represented by a measly ^, you fuzzball

  4. #4
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20
    Quote 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.

    Quote 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. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.
    Last edited by Daved; 10-04-2005 at 04:47 PM.

  6. #6
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20
    Quote 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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > a power can be represented by a measly ^, you fuzzball
    Not so.
    ^ is the exclusive or operator (a bitwise operator for integers only).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    OHNOES! A PURPLE ZOMBIE! Sennet's Avatar
    Join Date
    Sep 2005
    Posts
    20
    Quote 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 subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Interest program
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2001, 03:28 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM