### Program with classes

In my program I am using a class called Fraction and having the user input a numerator and a denominator and the solving the fraction and then the fraction gets reduced to lowest terms and printed. I am having trouble figuring out how to get the fraction to reduce and then print. This would go in the
`void Fraction::setFractReduce()` function.

Can someone help me? Tips, examples?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` `````` #include #include #include #include #include using namespace std; class Fraction { public: void setFractFinder(); void setFractAnswer(); void setFractReduce(); private: double numurator; double denomonator; double answer; }; int main (){ Fraction Fract; Fract.setFractFinder(); Fract.setFractAnswer(); Fract.setFractReduce(); } void Fraction::setFractFinder(){ Fraction Fract; cout << "Enter numerator:\n"; cin >> numurator; cout << "Enter denomonator:\n"; cin >> denomonator; if(denomonator == 0){ cout << "You cannot devide by zero!" << endl; exit(0); } } void Fraction::setFractAnswer(){ Fraction Fract; answer = numurator / denomonator; cout << "Your answer is: "; cout << answer << "\n"; } void Fraction::setFractReduce() { Fraction Fact; cout << "Fraction in lowest terms is: " << endl; } ``````
Last edited on
- Personally, i would divide the numerator and denominator by every number up until half of itself, then i would see if there are any common factors.
How would I do that here exactly?
I want to do this with a void function and I am having trouble with it.
closed account (3qX21hU5)
What do you have so far for the function? If we can see what you have we can push you in the right direction.

Also something I noticed doing a quick scan of the code you posted is this.

You declare a object of you class in every member function... Why? you don't need them there delete them. I think you are doing this so you can use the private variables of the class? If so you don't need to do that, every member function has access to the private variable of its class.
See that is where I am stuck. I am not sure how to start this function. I have seen some examples with bool functions but it does not go well to use double values with the % sign I guess.

And I understand about the declaring the object of my class in every function, it was habit but I understand that I do not need it there. Thank you.
nothing has changed much in my code except for getting rid of the unnecessary declaring of the object in each function.
closed account (3qX21hU5)
I suck at fractorials and specially decimal ones but here is a one that uses int's and solves your problem with recursion (Which is probably not the best way to go about it) maybe it can give you some ideas.

 ``1234567`` ``````int Fraction::setFractReduce(int n,int d) { if (d != 0) setFractReduce(d , n % d); //recursive call return n; //return n when d equals 0 }``````
Last edited on
- Make a for loop from zero until the number or half of that number, then modulus your number by the number being incremented.

-Any number that makes your result 0 is a factor of your original number.

- You could even keep a counter for how many times this happens, then make an array of that size and loop through again to get the numbers.

- There are a multitude of ways that you could go about solving this problem.
I'm not fond of your approach. Here's a minimal example of a different one.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` ``````#include #include #include #include #include class MyFraction { public: MyFraction( int num, int den ) : _numerator(num), _denominator(den) { if ( (_numerator < 0 && _denominator < 0) || _denominator < 0 ) { _numerator = -_numerator ; _denominator = -_denominator ; } } void reduce() ; int numerator() const { return _numerator; } int denominator() const { return _denominator; } operator double() const { return static_cast(_numerator) / _denominator; } private: int _numerator ; int _denominator ; }; MyFraction getFraction() { int numerator, denominator ; std::cout << "Enter numerator: " ; std::cin >> numerator ; std::cout << "Enter denominator: " ; std::cin >> denominator ; if ( denominator == 0 ) { std::cout << "Invalid denominator. Terminating program." << std::endl ; throw 1 ; } return MyFraction(numerator, denominator) ; } std::ostream& operator<<(std::ostream& os, const MyFraction & f ) { os << f.numerator() ; if ( f.denominator() != 1 ) os << '/' << f.denominator() ; return os ; } int gcd( int x, int y ) { if ( y != 0 ) gcd(y, x%y) ; return x ; } int main (){ MyFraction f = getFraction() ; f.reduce() ; std::cout << f << " is " << static_cast(f) << '\n' ; } void MyFraction::reduce() { int divisor = gcd(std::min(std::abs(_numerator),_denominator), std::max(std::abs(_numerator),_denominator)) ; _numerator /= divisor ; _denominator /= divisor ; }``````
So I am a beginner and was wondering
@cire what does `std::ostream& operator<<(std::ostream& os, const MyFraction & f )` mean? What is going on here?

@thejman250 Your suggestions were very helpful!! Thank you very much, those options are good.

@Zereo Thank you for that example it does give me a lot of ideas, I appreciate you helping out.
closed account (Dy7SLyTq)
the @cire: that is an overloaded operator that prints f
oh okay!! That makes sense.

How can I get a do while loop to work for this program? At the end i want it to ask the user if they want to find another fraction answer but I am not sure how? any hints?
A do while loop for the code above. Or another loop that will get it to repeat until the user says they are done.
I figured out the do while loop. Thank you everyone for your help on this. Things make a lot more sense now. Thank you again!!!
bruntmjust wrote:

bruntmjust (115) Mar 12, 2013 at 12:59am
So I am a beginner and was wondering
@cire what does std::ostream& operator<<(std::ostream& os, const MyFraction & f ) mean? What is going on here?

@thejman250 Your suggestions were very helpful!! Thank you very much, those options are good.

@Zereo Thank you for that example it does give me a lot of ideas, I appreciate you helping out.
bruntmjust wrote:
I figured out the do while loop. Thank you everyone for your help on this. Things make a lot more sense now. Thank you again!!!

- No problem.
Topic archived. No new replies allowed.