Amateur question.

This is a discussion on Amateur question. within the C++ Programming forums, part of the General Programming Boards category; This program is supposed to take the four inputs and use them to calculate the balance for each year of ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    28

    Amateur question.

    This program is supposed to take the four inputs and use them to calculate the balance for each year of the users N amount of years.

    Balance = P * (1 + I / Q ) ^ (N * Q)



    I've managed to do pretty much everything except get it to output the answers. I keep getting jumbled output. Any help would be greatly appreciated. Thanks.


    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    void screenHeader();                        // Parameterless Void Function
    void inputValues(float &p, float &i, float &q, int &n);     // Passed by reference
    float getBalance(float p, float i, float q, int n);        // Passed by Value
    
    
    int main()
    {
    
    int another = 1;
    float p, i, q;
    int n;                // Declaring Variables
    char answer;
    float balance;
    
    system("clear");            // Clears Screen
     
    screenHeader();            // Calling Void
     
    
    while(another != 0) {                            // While Loop
     
         system("clear");
         screenHeader();
         inputValues(p, i, q, n);
            
        
        cout << "--------------------------------------------" << endl;
        cout << "Principal amount======================> " << p << endl;
          cout << "Annual interest rate==================> " << i << endl;
          cout << "Times compounded per year=============> " << q << endl;
          cout << "Years to keep in account==============> " << n << endl;
     
    
       
      for(int count = 1; count <= n; count++)                    // For Loop
        {
          balance = getBalance(p, i, q, count);
          cout << "--------------------------------------------" << endl;
          cout << "Balance after " << count << " years: " << balance << endl;
        }
    
             cout << "--------------------------------------------" << endl;
         cout << "--------------------------------------------" << endl;
    
             cout << "Would you like to do another calculation? Enter Y or N: ";
             cin >> answer;
    
            if(answer != 'y' && answer != 'Y')
            another = 0;
    
    }                                              // end of while loop
     return 0;
    }
    
    
    
    void screenHeader()
     {
       cout << "Program SAVE" << endl;
       cout << "--------------------------------------------" << endl;
       cout << "This program will request that you enter"     << endl;
       cout << "values for:                     " << endl;
       cout << "                         " << endl;
       cout << "     a principal amount (P)                 " << endl;
       cout << "     an annual interest rate (I)            " << endl;            // Cout's
       cout << "     times compounded per year (Q)          " << endl;
       cout << "     years in account (N)            " << endl;
       cout << "                         " << endl;
       cout << "The program will then calculate and print   " << endl;
       cout << "the balance for each year up through year N." << endl;
       cout << "--------------------------------------------" << endl;
       cout << "                                            " << endl;
     }   
    
    
    
    
    void inputValues(float &p, float &i, float &q, int &n)
    {    
      cout << "Please enter your principal amount: "                   << endl;         // Inputting Values and Outputting Statements
      cin  >> p;
      cout << "Please enter the annual interest rate: "                << endl;
      cin  >> i;
      cout << "Please enter the times compounded per year: "           << endl;
      cin  >> q;
      cout << "Please enter the number of years in the account:  "     << endl;
      cin  >> n;
    }
    
    
    
    float getBalance(float p, float i, float q, int n)                    
    {
       return  (pow (( p*(1+ (i / q))),(n * q)));
    }

  2. #2
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    I think your formula for calculate the balance is getting to pretty big numbers (that get beyond the range for floating numbers in C). Declare double getBalance(float p, float i, float q, int n); and see what happens.
    Here is the result of your program on my compiler:
    Code:
    //my output
    This program will request that you enter
    values for:                     
                             
         a principal amount (P)                 
         an annual interest rate (I)            
         times compounded per year (Q)          
         years in account (N)            
                             
    The program will then calculate and print   
    the balance for each year up through year N.
    --------------------------------------------
                                                
    Please enter your principal amount: 
    Please enter the annual interest rate: 
    Please enter the times compounded per year: 
    Please enter the number of years in the account:  
    --------------------------------------------
    Principal amount======================> 5
    Annual interest rate==================> 6
    Times compounded per year=============> 7
    Years to keep in account==============> 8
    --------------------------------------------
    Balance after 1 years: 5.95261e+006
    --------------------------------------------
    Balance after 2 years: 3.54335e+013
    --------------------------------------------
    Balance after 3 years: 2.10922e+020
    --------------------------------------------
    Balance after 4 years: 1.25553e+027
    --------------------------------------------
    Balance after 5 years: 7.4737e+033
    --------------------------------------------
    Balance after 6 years: 1.#INF
    --------------------------------------------
    Balance after 7 years: 1.#INF
    --------------------------------------------
    Balance after 8 years: 1.#INF
    --------------------------------------------
    --------------------------------------------
    Would you like to do another calculation? Enter Y or N:
    (sorry for my bad english)

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Thanks, I tried that and this is my output using doubles instead of floats for getBalance. Seems like the same thing basically. Just not sure, what the issue is. It seems correct to me, but I don't know.







    Code:
    Program SAVE
    --------------------------------------------
    This program will request that you enter
    values for:
    
         a principal amount (P)
         an annual interest rate (I)
         times compounded per year (Q)
         years in account (N)
    
    The program will then calculate and print
    the balance for each year up through year N.
    --------------------------------------------
    
    Please enter your principal amount:
    1000
    Please enter the annual interest rate:
    4.0
    Please enter the times compounded per year:
    2
    Please enter the number of years in the account:
    5
    --------------------------------------------
    Principal amount======================> 1000
    Annual interest rate==================> 4
    Times compounded per year=============> 2
    Years to keep in account==============> 5
    --------------------------------------------
    Balance after 1 years: 9e+06
    --------------------------------------------
    Balance after 2 years: 8.1e+13
    --------------------------------------------
    Balance after 3 years: 7.29e+20
    --------------------------------------------
    Balance after 4 years: 6.561e+27
    --------------------------------------------
    Balance after 5 years: 5.9049e+34
    --------------------------------------------
    --------------------------------------------
    Would you like to do another calculation? Enter Y or N:

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    > Annual interest rate==================> 4
    This is like 4000% or something, which will grow the values unbelievably fast.

    IIRC, a 4% interest rate is written as either 1.04 or 0.04
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Yeah you're right. However the same problems remain when I use what four percent actually would be.





    Code:
    Program SAVE
    --------------------------------------------
    This program will request that you enter
    values for:
    
         a principal amount (P)
         an annual interest rate (I)
         times compounded per year (Q)
         years in account (N)
    
    The program will then calculate and print
    the balance for each year up through year N.
    --------------------------------------------
    
    Please enter your principal amount:
    1000
    Please enter the annual interest rate:
    0.04
    Please enter the times compounded per year:
    2
    Please enter the number of years in the account:
    5
    --------------------------------------------
    Principal amount======================> 1000
    Annual interest rate==================> 0.04
    Times compounded per year=============> 2
    Years to keep in account==============> 5
    --------------------------------------------
    Balance after 1 years: 1.0404e+06
    --------------------------------------------
    Balance after 2 years: 1.08243e+12
    --------------------------------------------
    Balance after 3 years: 1.12616e+18
    --------------------------------------------
    Balance after 4 years: 1.17166e+24
    --------------------------------------------
    Balance after 5 years: 1.21899e+30
    --------------------------------------------
    --------------------------------------------
    Would you like to do another calculation? Enter Y or N:

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    > Balance = P * (1 + I / Q ) ^ (N * Q)

    > return (pow (( p*(1+ (i / q))),(n * q)));
    How about
    return p * pow ( (1+ (i / q))), (n * q)) );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Won't Compile while using :
    return p * pow ( (1+ (i / q))), (n * q)) );


  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    Just balance the () with the p* outside of the pow.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    try taking away the last ), I think that's one too many.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Still isn't compiling. Not sure at all what else to do.

  11. #11
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    Try this:
    Code:
    double getBalance(float p, float i, float q, int n)                    
    {
       return (p* pow ( (1+ (i / q)),(n * q)));
    }
    It's working OK for me...

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Awesome, Thanks a ton! I knew it was something minor I just couldn't handle sitting there any longer. Needed some outside input. Thanks for all who helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. very amateur question:
    By jemer in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2003, 02:41 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21