Class issue AGAIN!

This is a discussion on Class issue AGAIN! within the C++ Programming forums, part of the General Programming Boards category; I know I cannot get anymore basic then this. Which is frustrating, but I thought I would make one more ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    486

    Class issue AGAIN!

    I know I cannot get anymore basic then this. Which is frustrating, but I thought I would make one more project to make sure I got it. Then line 68 gave me a error when I was just about done. I am getting a warning on line 41 as well.

    Also be to clear when I separate the code into different files I take the main syntax and put it into the .h file, call the functions in the .cpp file and just add the user input into main in the main file.

    I wonder how long it will take until I am good at this. My textbook gave me a bonus question to answer is I was up for the challenge about making a cryptography code and I tired but failed. It was on Chapter 4 out of 23 chapters, so that tells me the little hamster wheels up there needs some tuning. I'll keep trying.

    Code:
     #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class compoundInterest {
        
    public:
        explicit compoundInterest(double inital, double percentage, int years)
        :principle(inital),rate(percentage),time(years)
        {
        
        }
        void startingAmount(double inital)
        {
            principle=inital;
        }
        void percent(double percentage)
        {
            rate=percentage;
        }
        void lenth(int years)
        {
            time=years;
        }
        double getPrinciple() const
        {
            return principle;
        }
        double getPercentage() const
        {
            return rate;
        }
        double getYears() const
        {
            return time;
        }
        int calculate()
        {
            int total;
            total = principle * (1 + rate/100);sqrt(time);
            
            return total;
        }
        
    private:
        double principle;
        double rate;
        int time;
    };
    int main(int argc, const char * argv[])
    {
        double principle;
        double rate;
        int time;
        
        compoundInterest CompoundInterest(double principle, double rate, int time);
    
        cout<<"Enter the Priciple: "<<endl;
        cin>>principle;
       
        cout<<"Enter the Rate:"<<endl;
        cin>>rate;
        
        cout<<"Enter the duration of the loan:"<<endl;
        cin>>time;
        
        cout<<"After "<<time<< " you will have paid a total of "<<CompoundInterest.calculate()<<endl;
        
        
        return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    #1 - This line:
    Code:
    compoundInterest CompoundInterest(double principle, double rate, int time);
    is a prototype for a function named CompoundInterest that returns a value of type compoundInterest. It is not an object named CompoundInterest of type compoundInterest which is what you want. There is a difference between that and:
    Code:
    compoundInterest CompoundInterest(principle,rate,time);
    #2
    Code:
    int main(int argc, const char * argv[])
    {
        double principle;
        double rate;
        int time;
         
        compoundInterest CompoundInterest(double principle, double rate, int time);
     
        cout<<"Enter the Priciple: "<<endl;
        cin>>principle;
        
        cout<<"Enter the Rate:"<<endl;
        cin>>rate;
         
        cout<<"Enter the duration of the loan:"<<endl;
        cin>>time;
    Here you input values into your variables but this occurs after your failed attempt to create an object so even if you got that right it still wouldn't work since the variables would first need to have valid values before you sent them to your object.

    You need to correct the creation of your object and then either put that creation step after the user has entered values or use the length/percent/startingAmount member function to set the appropriate values before you call the calculate member function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    486
    Awesome thanks, now I have to work on line 41. Its giving 0 no matter what. I wanted to use ^ but it wouldn't work even after I added the math .h file. So I took a chance and used something I learned from C and only got a warning but my compiler made me put a ; in a odd spot.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    486
    Code:
     double calculate()
        {
            int total;
            total = principle * (1 + rate/100 * sqrt(time));
            
            return total;
        }
    Still getting a 0....

    What do you think I am using wrong?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    You are using integer division. Change rate/100 to rate/100.0 to use floating point division (otherwise decimals are thrown away). So long as at least one side of a mathematical expression is a float or double, it's a float point operation; otherwise it's an integer operation (decimals are thrown away).
    Also, I advise you to not name variables "time" because there is a function in the standard library named time. Or, you could stop using using namespace std and start prefixing everything with "std::". Your choice.
    Also, math.h should be cmath.

    Code:
    double calculate()
       {
           int total;
           total = principle * (1 + rate/100 * sqrt(time));
            
           return total;
       }
    You really don't need to do this. You can initialize a variable on the same line as you're declaring it on, so it could be simplified to

    int total = static_cast<int>( principle * (1 + rate/100.0 * sqrt(time)) );
    return total;

    But you can put an expression into return directly; it does not require a variable, so you can simplify it further:

    return static_cast<int>( principle * (1 + rate/100.0 * sqrt(time)) );

    Also think about whether you want to keep the decimals or not.
    Last edited by Elysia; 09-10-2013 at 03:34 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ thread inside a class hang issue
    By diefast9 in forum C++ Programming
    Replies: 11
    Last Post: 10-21-2009, 08:18 AM
  2. C++ Newbie - Derived Class Issue
    By Gipionocheiyort in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2007, 01:20 AM
  3. Issue with a templatized class
    By ruthgrin in forum C++ Programming
    Replies: 7
    Last Post: 02-19-2006, 02:22 AM
  4. Class issue
    By PunkyBunny300 in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2003, 07:35 PM
  5. An class structure issue
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 12-31-2002, 04:56 PM

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