# Class issue AGAIN!

• 09-10-2013
jocdrew21
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; }```
• 09-10-2013
hk_mp5kpdw
#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.
• 09-10-2013
jocdrew21
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.
• 09-10-2013
jocdrew21
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?
• 09-10-2013
Elysia
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)) );