1. You need to cast fraction1.numerator and fraction1.denominator to floats separately, because otherwise you'll end up with incorrect results because integer division is truncating.
equivalent1 = ((float)fraction1.numerator / fraction1.denominator);
will result in e.g. 2.0 where fraction1.numerator is 5 and fraction1.denominator is 2 (even though the result should be 2.5, you're diving two integers and then casting the result to a float, which gives 2.0). You want sometihng more like equivalent1 = ((float)fraction1.numerator) / ((float)fraction1.denominator);
2. Why are equivalent1 and equivalent2 being assigned after you print them?
Its because of lines 16 and 17. You are assigning to equivalent1/2 the answer of the division of two variables which do not hold a value that you have assigned.
Put line 16 in between lines 26 and 27.
Put line 17 in between lines 33 and 34.
Ok I think you got your lines 16 and 17 confused with 19 and 20. Moving the declaration or the equivalent variables down into the process this time causes the program to execute the second fraction perfectly but the first one still doesn't come out right. I don't understand why it seems like the problem is in the fraction1.denominator variable. It's getting passed first, it seems like it shouldn't have any issue with the value stored in it.
Yes you're correct I did mean lines 19 and 20, I apologize. I don't understand why you are casting to double; it has no effect on the number whatsoever. The numerator/denominator values are of type int so even if you type in a value such as 4.33333333 it gets truncated to 4. So when you cast it to float later on it is still 4. Change the members to type float and remove the casts.
I don't understand why you are casting to double; it has no effect on the number whatsoever.
It has an effect on the resulting division. If both numbers are integers, then it will do integer division, but if one (or both) is floating point, the result will be floating point.
It has an effect on the resulting division. If both numbers are integers, then it will do integer division, but if one (or both) is floating point, the result will be floating point.
I thought that if the variable being assigned the result was type float it wouldn't matter; guess I was wrong...
1. You need to cast fraction1.numerator and fraction1.denominator to floats separately, because otherwise you'll end up with incorrect results because integer division is truncating.
lines 21,22 probably meant to be setting fraction1.denominator .. not fraction2.
I can't believe I didn't see this. I could've swore I looked over it 10 times and didn't see it. Apparantly no one else caught it either! lol That fixed that problem!
Ok. Now I'm adding the second part of the program, which is to calculate the sum of both the fractions by multiplication. Could one of you run the code to see if you can identifiy the calculation error? I'm also considering possibly calculating the common denominator in somehow, any ideas?
wow. i can't believe i overlooked that. Now i understand that with programming, learning to catch your logic errors can be as much a task as writing the code itself. thank you