Do these have to be double? Ints would be a lot easier. You shouldn't make direct comparisons between doubles, because of the binary fraction representation - not all real numbers can be stored exactly, so direct comparisons quite often fail.
To do this assignment, you also need a Greatest Common Denominator function, so you can rationalise fractions such as 4 / 6 into 2 /3.
#include <iostream>
usingnamespace std;
int main ()
{
int num, denom;
char ch1;
cout << " Please enter a fraction (numerator / denominator) " << endl;
cin >> num >> ch1 >> denom;
cout << "Converted number: ";
if (num % denom == 0)
{
cout << num/denom << endl;
}
elseif (num < denom)
{
cout << num << "/" << denom << endl;
}
elseif (num > denom)
{
cout << num/denom << " " << num%denom << "/" << denom << endl; //Dividing an int by an int will just truncate: 5/2 = 2. 8/3 = 2 and so on.
}
return 0;
}
I would recommend something like this. Please look up the MODULUS operation (%), as it's very useful for this. It returns the remainder after division. Good luck and hopefully you can stick with your class!
E: You may also wish to modify this to check for a denominator that is ZERO before performing any division with it. Sorry. And don't worry about reducing the fractions -- the prompt gives an example output of "10/12", which is not reduced.