Compound Interest Program

Discussion in 'C++' started by veronicak5678, Sep 16, 2007.

  1. veronicak5678

    veronicak5678 New Member

    Joined:
    Sep 16, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I can't seem to figure this out, though it should be pretty simple. Here are my instructions:
    Compound interest

    Compound.cpp

    Write a program that calculates compound interest. The program should ask the user for the starting dollar amount and the daily increase (as a percentage), and the number of days. A loop should then be used to display the day, the amount of interest earned on that day and the account balance on that day. The program should also display the total interest earned. Output should be as follow



    Initial amount in dollars? 100
    Interest rate in percentage? 10
    Number of days? 3




    Day Earned interest Balance
    -----------------------------------------------
    1 $10 $110.00
    2 $11 $121.00
    3 $12.10 $133.10

    Total Interest earned: $33.10


    Validation:
    Dollar amount should be between 10 and 10000
    Interest rate should be between 1 and 22
    Number of days should be between 2 and 30

    The output should be formatted and aligned according to the above.


    Here is my code:

    Code:
    
    
    
    #include <cstdio>
    #include <math.h>  
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    double dollars, amount, interest, power, formularate; 
    int days, rate;
    bool notValid;
    int a=1;
       
       do{
       notValid=false;                                       //Validation for amount
       cout << "\nPlease enter dollar starting amount?" ; 
       cin >> dollars;
       if (dollars <10 || dollars >10000)
           {
            cout << "\nYou have entered invalid data"; 
            notValid=true;
            }
       } while (notValid);
    
       do{
       notValid=false;                                       //Validation for rate
       cout << "\nWhat is the daily increase (as a percentage)?" ;
       cin >> rate;
       if (rate <1 || rate >22)
           {
            cout << "\nYou have entered invalid data"; 
            notValid=true;
            }
       } while (notValid);
      
       do{
       notValid=false;                                       //Validation for days
       cout << "\nEnter the number of days:" ;
       cin >> days;
       if (days<2 || days >30)
           {
            cout << "\nYou have entered invalid data"; 
            notValid=true;
            }
       } while (notValid);
      
       {
           cout << "\n\nDay     Earned Interest     Balance"; 
           cout << "\n-----------------------------------\n";
            
           
            for(a==1; a <= days; )                           //Loop for display 
                    { 
                      formularate=(rate/100);
                      amount=dollars*pow(1+(formularate/365),(a));
                      interest=amount-dollars;                     
                      cout << a;
                      cout << "           ";  
                      cout << interest; 
                      cout << "               "; 
                      cout << amount;  
                      cout << "               "; 
                      cout <<"\n\n";                  
                      a++;                                   //Increments a
                      }      
                system("pause");
                
    
        }
    }
    
    Why is my interest 0?
     
  2. HowardL

    HowardL New Member

    Joined:
    Aug 5, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    East Coast USA
    When you have myteries like that print out some values to see what is going on.
    I did that and found (and noted) a couple of things that should get you going.
    Code:
      {
        cout << "\n\nDay Earned Interest Balance";
        cout << "\n-----------------------------------\n";
    
        /*  Three things here: 
            1) You have two conditionals.
            2) And you can increment a here! That IS one of the advantages of a for()
        for(a==1; a <= days; )  / * Loop for display                            */
    
        for(a = 1; a <= days; a++)  /* Loop for display */
        {
          /* 3) rate is declared as int.  You must declare or cast to a double.
          formularate = (rate / 100);     I just cast it below                 */
    
          formularate = ( (double)rate / 100);
    
          amount = dollars * pow(1 + (formularate / 365), (a) );
    
          interest=amount-dollars;
    
          cout << a;
          cout << " ";
          cout << interest;
          cout << " ";
          cout << amount;
          cout << " ";
    
          cout <<"  dollars= ";
          cout << dollars;
          cout <<"  days= ";
          cout << days;
          cout <<"  rate= ";
          cout << rate;
          cout <<"  formularate= ";
          cout << formularate;
    
          cout <<"\n\n";
          /* a++;             Increments a  (now done above in for() */
        }
    
    Output:
    Code:
    D:\C\Cprogforum\veronicak>  compound.exe
    
    Please enter dollar starting amount?1000
    
    What is the daily increase (as a percentage)?10
    
    Enter the number of days:10
    
    
    Day Earned Interest Balance
    -----------------------------------
    1 0.273973 1000.27   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    2 0.54802 1000.55   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    3 0.822143 1000.82   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    4 1.09634 1001.1   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    5 1.37061 1001.37   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    6 1.64496 1001.64   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    7 1.91939 1001.92   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    8 2.19388 1002.19   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    9 2.46846 1002.47   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    10 2.74311 1002.74   dollars= 1000  days= 10  rate= 10  formularate= 0.1
    
    From the additional values being printed I could see that rate was right but formularate was not , so that quickly became my focus. Have fun,
    Howard;
    ps: I don't know cpp , how do you format the numbers for cout << ?
     
  3. veronicak5678

    veronicak5678 New Member

    Joined:
    Sep 16, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Howard! You have helped a lot. And I don't really know C++ either! That's why my formatting is all weird.
     
  4. HowardL

    HowardL New Member

    Joined:
    Aug 5, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    East Coast USA
    I see there are many C++ tutorials at the top of this forum listing.
    I tried one and found the following which should help you tidy up the output:
    Code:
    /*   formout.cpp
    http://www.4p8.com/eric.brasseur/cppcen.html
    33.
    This program performs formated output two different ways.
    Please note the width() and setw() MODIFIERS are only effective on the
    next item output to the stream.  The second next item will not be influenced.
    
    my compiling notes:
    g++ -Wall -W -pedantic formout1.cpp -o formout1.exe
    ls -l
    -rw---a-      704  7 Sep 17 12:29:54 formout1.cpp
    -rwx--a-   424529  7 Sep 17 12:39:32 formout1.exe    1/2 meg ?! geez
    */
    
    using namespace std;
    #include <iostream>
    #include <iomanip>   /* looks like this is needed for setw() */
    
    int main ()
    {
       int i;
    
       cout << "A list of numbers:" << endl;
       for (i = 1; i <= 1024; i *= 2)
       {
          cout.width (7);
          cout << i << endl;
       }
    
       cout << "A table of numbers:" << endl;
       for (i = 0; i <= 4; i++)
       {
          cout << setw(3) << i << setw(5) << i * i * i << endl;
       }
    
       /*** (HL) I modified that loop and found we can do this:  ***/
       cout << "Mixed formatted numbers and text:" << endl;
       for (i = 0; i <= 4; i++)
       {
          cout << "i= " << setw(3) << i << setw(20) << "i * i * i= "
               << setw(5) << i * i * i << endl;
       }
    
       return 0;
    }
    
    And that output is:
    Code:
    D:\CPP\formatted_output>  formout1.exe
    A list of numbers:
          1
          2
          4
          8
         16
         32
         64
        128
        256
        512
       1024
    A table of numbers:
      0    0
      1    1
      2    8
      3   27
      4   64
    Mixed formatted numbers and text:
    i=   0         i * i * i=     0
    i=   1         i * i * i=     1
    i=   2         i * i * i=     8
    i=   3         i * i * i=    27
    i=   4         i * i * i=    64
    
    Well that's a start. . I suppose there are many more formatting options to be learned.
    New thing for the day!...
    ++Howard;
     
  5. veronicak5678

    veronicak5678 New Member

    Joined:
    Sep 16, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thanks so much for your help! I finished the program and it looks good.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice