Some problems with integer fractions

Hi guys; posted this before but apparently I completely fucked up my assignment.
Scored 18 out of a possible 120...

I've been trying to account for the problems but I just can't do it..
Here's the simple 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)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main ()
{
  int num, denom; // Declaring num as numerator and denom as denominator                                                
  char ch1;  // Declaring the character needed for slash                                                                
  cout << " Please enter a fraction (numerator / denominator) " << endl;
  cin >>  num >> ch1 >> denom;
  if (num % denom == 0) // If the fraction can be solved then solve it.                                                 
    {
      cout << num << ch1 << denom << " " << "=" << " ";
      cout << num/denom << endl;
    }
  else if (num < denom) // If the numerator is less than the denominator then simply display it.                        
    {
      cout << num << ch1 << denom << " " << "=" << " ";
      cout << num << "/" << denom << endl;
    }
  else if (num > denom) // If the numerator is greater than the denominator then solve it and reformat it in standard f\
ormat.                                                                                                                  
    {
      cout << num << ch1 << denom << " " << "=" << " ";
      cout << num/denom << " " << num%denom << "/" << denom << endl;
 else if (denom == 0) // If the denominator is equal to zero then display a error message.                             
    {
      cout << num << ch1 << denom << " " << "=" << " ";
      cout << (num/denom);
      cout << "You can't divide by 0!" << endl;
    }
  else if (num == 0) // If the numerator  is zero then display 0.                                                       
    {
      cout << num << ch1 << denom << " " << "=" << " ";
      cout << "0" << endl;
    }
  else if ( num > 0 && denom > 0) // If both the numerator and denominator are less than zero then find the absolute va\
lue and display the fraction.                                                                                           
    {
      num = abs(num);
      denom = abs(denom);
      cout << num << ch1 << denom << " " << "=" << " ";
      cout << abs(num/denom) << endl;
    }
return 0;
}


I need to do the following:
When displaying a fraction in this manner,
If the fraction is negative, the negative sign should precede the numerator, not the denominator. For example, 2 / -3 should be displayed as -2/3.

If the fraction is positive, both the numerator and denominator should be positive. For example, -4 / -5 should be displayed as 4/5.

If the denominator is 0, the fraction is not valid.

If the fraction is improper, the fraction should be converted to a whole or mixed number and, if negative, the negative sign should precede the whole number. For example, -23 / 4 should be displayed as -5 3/4. 16/8 should be 2.


This is what I need to fix:
-Account for zero in the denominator
-Account for zero in the numerator
-Find the absolute value if necessary.

Right now I get a floating point error if I try to account for zero in either the numerator or denominator.
Also if I have a fraction of the following: 13/5 then my program will correctly display 2 3/5.
But if I have a fraction of the following: -13/5 then my program doesn't display it properly.
Last edited on
Here you go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

int main ()
{
	int num, denom;
	char ch1;

	cout << "Please enter a fraction (numerator / denominator):  ";
	cin >>  num >> ch1 >> denom;

	cout << num << ch1 << denom << " = ";

	if (denom == 0)
		cout << "Invalid fraction!";

	else
	{

		if (num % denom == 0)
			cout << num/denom;

		else
		{
			if (num*denom < 0)
				cout << "-";

			if (abs(num) < abs(denom))
				cout << abs(num) << ch1 << abs(denom);
			else if (abs(num) > abs(denom))
				cout << abs(num/denom) << " " << abs(num)%abs(denom) << ch1 << abs(denom);
		}

	}

	cout << endl;

	return 0;
}


Learn by example. There are probably other / better ways of doing it but this is what I came up with on the spur of the moment.
Last edited on
Sweet thanks for the response! I really appreciate it.;
I have a quick question though.
What does the "*" character do on line 26?

Edited: Sorry just read that; multiplication der...
could you explain line 26 though?
is it:
If the numerator times the denominator is less than 0 then display -?
Is that correct?
Last edited on
1
2
3
4
5
6
else if (denom == 0) // If the denominator is equal to zero then display a error message.                             
    {
      cout << num << ch1 << denom << " " << "=" << " ";
      cout << (num/denom);
      cout << "You can't divide by 0!" << endl;
    }


I would just like to comment on the brilliance of writing some code that demonstrates clearly that you understand that you can't divide by zero, but still tries to do so anyway (leading to your floating point error).
Yes, if the numerator times the denominator is less than 0, then display a minus sign. Then just ignore the signs of the numerator/denominator after that point and only use the absolute value for calculations. This really quickly accounts for negative numerators and negative denominators and any combination thereof. If both are negative it won't display a negative sign and will go on to either display a proper fraction or a mixed fraction. And so on for other possibilities.
Last edited on
Wow you're code's pretty brilliant. I was thinking I need to account a specific if statement for every possible scenario but your condition on line 26 pretty much handles it.
The only thing is I need to display the original inputed fraction and then the reformatted version of it.
That's why I had all those cout statements.

So I need to try to input something like this:
cout << num << ch1 << denom << " = " <<;

Before each reformatting.
But if I try to incorporate something like this on your condition I just can't seem to get it.

Again thank you so much for your insight. I'll continue working on it.

Edit:
So I'm just talking this out in order for me to make sense of it.
Your condition states the following:
If the value of fractions is less then zero then there is at least one value which is negative.
We are going to simply print a - character before the fraction and treat the values as absolute.

But if I include my original cout statement that displays the original fraction, then there are two '-' characters that get displayed and that messes up the computation.

So we need to specify the condition or make a case that allows to only display the - character in the right scenario.

Edit 2:
Sooo we just need to make the numerator positive before applying the - sign so we just need to define a new variable and make it the absolute value of the numerator!!!

Edit 3:
Holy shit ya you did; I should have just tried compiling and testing it first.
Man... this has been a long day...

Edit 4:
I would love to simply copy your code but I just can't. It wasn't my idea and I can't copy it and pretend it was, that whole plagiarism thing...Thank you for your invaluable insight though!
Last edited on
Mine actually already does do that -- Line 13.
Topic archived. No new replies allowed.