Thread: help with compounded interest

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    14

    help with compounded interest

    The code below was supposed to display interest yearly like:
    Year 1 $1000
    Year 2 $1100
    Year 3 $1210

    Problem is it will display the value for the last year for all 3 years... what am i doing wrong?


    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    
    //Declare variables
    double percent_return = 0;
    double investment = 0;
    int nYears = 0;
    double value = 0;
    int count = 0;
    
    
    //Ask user for initial amount 
    cout << "Enter the initial amount of the investment: " << endl;
    cin >> investment;
    
    //Ask user for interest rate
    cout << "Enter the interest rate: " << endl;
    cin >> percent_return;
    
    //Ask user for number of years
    cout << "Enter the number of years: " << endl;
    cin >> nYears;
    
    for(count = 1; count <= nYears; count++)
    {
    
    value = investment * pow(1 + percent_return, nYears);
    
    cout << value << endl;
    
    }
    
    
    
    return 0;
    
    }
    Last edited by trippedwire; 09-25-2004 at 10:07 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Please edit your post and use code tags.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    Sorry.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    value = investment * pow(1 + percent_return, nYears);
    The problem is on that line
    investment never changes
    percent_return never either
    neither does nYears
    Well, something got to change during the iteration of the loop. What would that be?
    Welcome to the board
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    i don't want nYears to be constant....

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    value = investment * pow(1 + percent_return, count);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    14
    ah, okay.... makes sense. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming project help?
    By Debbie Bremer in forum C++ Programming
    Replies: 21
    Last Post: 10-11-2008, 02:48 AM
  2. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 11-01-2007, 04:26 AM
  3. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 09:09 PM
  4. Compound Interest Formula
    By veronicak5678 in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2007, 05:16 PM
  5. Interest program
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2001, 03:28 AM