Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is probably a fairly simple task, but for some reason, I can\'t get this to

ID: 3629534 • Letter: T

Question

This is probably a fairly simple task, but for some reason, I can't get this to work. I am writing a small program (learning about classes). I have a class called Rational, with 2 private member variables ( int numerator and int denominator).

I have some public member functions, one of which I need to use to display the floating point value for the fraction, displaying to the thousandths place. The numerator and denominator are passing to the function just fine, but the division does not seem to be taking place. Here is my function (defined outside the class "Rational"):

void Rational :: dispAsDec(Rational r1) //allows me to define function outside class
{
float result = r1.numerator / r1.denominator;
cout << setw(5) << fixed << setprecision(3) << result << endl;
}

Does anyone know why it is not performing the division? For example, if the fraction is 4/5, result should equal 0.800, but in my program, it is just saying 1.000 for pretty much everything.






Explanation / Answer

I believe that your methods "numerator" and "denominator" are returning Int type values. If so, then you are doing integer division in which 4/5=1. Try changing the class type and return value to a double to a float.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote