### FML I don't know shit about logic

So this assignment's due in about an hour and 15 minutes...
And I'm just out of my mind with frustration.
So out of desperation I'm pleading for help

Here's the prompt:
 Design a C++ program to convert fractions into one of the following standard formats (whichever is appropriate): * numerator/denominator (4/7) * whole_number numerator/denominator (3 10/12) * whole_number (-83) .

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include using namespace std; int main () { double num, denom; int whole; char ch1; cout << " Please enter a fraction (numerator / denominator) " << endl; cin >> num >> ch1 >> denom; if ( num == denom && denom == num) { whole = num/denom; cout << whole << endl; } if ( num >= 0 && denom >= 0) { whole = num/denom; cout << whole << endl; } if ( num <= 0 && denom <= 0) { whole = num/denom; cout << whole << endl; } if (num <= 0 && denom >= 0) { whole = num/denom; cout << whole << endl; } if (num <=0 && denom >= 0) { static_cast(whole = num/denom); cout << whole << endl; } return 0; } ``````

My if statements don't account for a lot of things...
Last edited on
`double num, denom;`

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.

HTH Good Luck
 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace 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; } else if (num < denom) { cout << num << "/" << denom << endl; } else if (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.
Last edited on
Bro I love you.
No words...
Topic archived. No new replies allowed.