Whats the problem here?!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Whats the problem here?!

  1. #1
    Join Date
    Apr 2004
    Posts
    136

    Whats the problem here?!

    Oook..im asked to create a savings calculation program. using a user defined contructor to initialise the values & also to set the values. Prior to this I used to constructor to initialise the values to 0/0.0 ect & then i had set methods to assign the values. After reading the briefing no set methods were to be used!

    soooo I moddifed my program but now the output calculations are wrong? and i cant see why :( ....can some 1 point me to where or why this is happening plz

    -------------------------------------
    Header
    ------------------------------------
    class Savings{
    private:
    double Investment;
    double Rate;
    int Period;

    public:
    Savings(double InvIn, double RateIn, int PeriodIn);
    double getFinalValue();
    };
    -----------------------------------------
    Implementation
    -----------------------------------------
    # include "Savings.h"


    Savings::Savings(double InvIn, double RateIn, int PeriodIn ){

    Investment=InvIn;
    Rate=RateIn;
    Period=PeriodIn;
    }


    double Savings::getFinalValue(){

    for (int i=0; i<=Period; i++) //int i=1 makes answer wrong aswell
    Investment +=(Investment*Rate/100);
    return Investment;
    }

    -----------------------------------------------------
    main cpp
    -----------------------------------------------------
    # include <iostream.h>
    # include <conio.h>
    # include "Savings.h"


    void main()
    {
    double Inv=0.0, Rate=0.0;
    int Period=0;

    cout<<"Investment = ";
    cin>>Inv;

    cout<<""<<endl;

    cout<<"Yearly Interest Rate = ";
    cin>>Rate;

    cout<<""<<endl;

    cout<<"Saving Duration(Years) = ";
    cin>>Period;

    Savings NewSavings(Inv, Rate, Period);//Create new object of the savings class
    //& Assign data values...
    clrscr(); //Clear the screen

    cout<<"Investment():";
    cout.width(29); //allign the values for tidiness
    cout<<Inv<<endl;

    cout<<"Yearly Interest Rate(%):";
    cout.width(18);
    cout<<Rate<<endl;

    cout<<"Duration(Years):";
    cout.width(27);
    cout<<Period<<endl;

    cout<<"Amounts to a final calculation of:";
    cout.width(8);
    cout<<""<<NewSavings.getFinalValue();
    }
    --------------------------------------------------------------------------

    Was giving the correct answer before i changed it to remove the set methods. Now when im passing the values via paramater to the class the output answer is wrong wrong wrong
    Oh why why why :confused:

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi mate,

    probably your formula is wrong:

    for (int i=0; i<=Period; i++) //int i=1 makes answer wrong aswell

    This actually iterates Period + 1 times, so if you calculate the value for a year (Period == 1) you iterate through the loop twice!

    another tip: When initializing in a constructor, get used to use an initializer list. It's tidier and more efficient in most cases.

    Savings::Savings(double InvIn, double RateIn, int PeriodIn )
    : Investment(InvIn),
    Rate(RateIn),
    Period(PeriodIn)
    {

    }

    and if you want to use some statndard values for initialization try this:

    Savings::Savings(double InvIn = 100.0 /* in */, double RateIn = 0.05 /* 1 == 100% */, int PeriodIn = 10 /* in years */)
    : Investment(InvIn),
    Rate(RateIn),
    Period(PeriodIn)
    {

    }

    Cheers,

    D

  3. #3
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    And another typo:
    double literals in c/c++ are normally told apart from integer literals by a .0
    You divide "Rate/100" which should be "Rate/100.0". There are some rules where you get away with such sloppiness because of automatic type conversion, but better be tidy there.

    D

  4. #4
    Join Date
    Apr 2004
    Posts
    136
    all fixed......
    i assumed the windows calculator was itterating the +% ontop of the new sum from the oprevious +%

    seems it wasnt!!!

    Cheers for your help with the contructor initialisation & the itteration prob :WAVE:
    Last edited by process; 03-15-2005 at 04:47 PM.

  5. #5
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    I got this in my email:
    not: 1% of 10 is 0.1 so your result is correct...

    ok, ive fixed the itteration problem

    for (int i=1; i<=Period; i++)
    Investment +=(Investment*Rate/100);

    that initialisation method is new to me & i have used it.Thanks :D

    :Investment(InvIn),
    Rate(RateIn),
    Period(PeriodIn)
    {
    }

    But the problem still here...My calculation above must be wrong?
    ie: if i enter the following:

    investment = 10
    rate = 1%
    period = 1

    I get the answer 10.1
    but it should be 11

    :confused:
    ***************

  6. #6
    Join Date
    Apr 2004
    Posts
    136
    ye lollol!..sorry that was a moment of thickness

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center