Monday, May 24, 2010

Whats wrong with this c++ program?

the answerr that always comes out is one.





class alvarez{


protected:


double Inter;


double time;


double percentage;


public:


void CalcArea();


void ShowArea();


alvarez(double I=0, double t=0);


};


alvarez::alvarez(double I,double t)


{


I=Inter;


t=time;


}


void alvarez::CalcArea()


{


percentage= Inter/time;


}


void alvarez::ShowArea()


{


cout %26lt;%26lt;"the percentage is:"%26lt;%26lt; percentage %26lt;%26lt;endl;


}





#include%26lt;iostream.h%26gt;


#include "alvarez.h"





class ThreeD: public alvarez


{


protected:





double Percentage;





public:


void CalcVol();


void ShowVol();


ThreeD (double I=0, double t=0);


};





ThreeD :: ThreeD(double I, double t):alvarez(I,t)


{





}


void ThreeD::CalcVol(){


Percentage=Inter/time;


}


void ThreeD::ShowVol(){


cout %26lt;%26lt; "The percentage is: " %26lt;%26lt; Percentage %26lt;%26lt; endl;


}





main(){


double I,t;


cout %26lt;%26lt; "Enter I: ";


cin %26gt;%26gt; I;


cout %26lt;%26lt; "Enter t:";


cin %26gt;%26gt; t;


ThreeD box (I,t);


box.CalcVol();


box.ShowVol();


return(0);


}

Whats wrong with this c++ program?
The result from the above-stated codes will always be 1 because zero divided by zero is ONE. (0 / 0 = 1).





Take a better look at your constructor that takes in two arguments 'l' and 't'. It should be the integer variables 'Inter' and 'time' that receives the value of the arguments l and t and not vice versa...





This is how to solve it...





alvarez::alvarez(double l, double t)


{


Inter = I;


time = t;


}

tropical flowers

No comments:

Post a Comment