I need some help on the void plusEquals/minusEquals and etc function
I'm not sure how to start it
Please can anyone help me do the plusEquals function so I know what I'm suppose to do or maybe some guides on how to approach it
Below the problem is my current progress code on the problem

Thank You!!

The Problem:
Write a class definition for a fraction class. Each variable of type fraction represents a single fraction. The class has two member variables that should be private: the numerator and the denominator of the fraction. The class has the following public member functions:

1. void plusEquals(fraction second); // adds second to the member fraction and stores the result in the member fraction
2. void minusEquals(fraction second); // substracts second from the member fraction and stores the result in the member fraction
3. void timesEquals(fraction second); // multiplies second and the member fraction and stores the result in the member fraction
4. void dividesEquals(fraction second); // divides the member fraction by second and stores the result in the member fraction
5. double toDecimal(); // returns the decimal value of the member fraction
6. void input(); // read a fraction of the form x/y from the user
7. void output(); // print the fraction in the form x/y to the screen
8. fraction(); // constructor that defines a default fraction of 0/1
9. fraction(int n, int d); // constructor that defines a fraction n/d

The class should also define one private member function:

1. void reduce(); // reduce the member fraction to simplest terms, which should be done automatically any time that the value of the member fraction changes

Note that the reduce function should use Euclid’s method (as we did in Lab 7) to find the greatest common divisor of the numerator and denominator. You may do that directly in the reduce function, or you may add another private member function to the class just to calculate the GCD. You will also have to write a driver program to test the fraction class. Be sure that you test all the member functions for correctness.

#include <iostream>
using namespace std;

class fraction
{
public:
	void plus_equals()
	{
		
	}
	void minus_equals()
	{

	}
	void times_equals()
	{

	}
	void divide_equals()
	{

	}
	double to_decimal()
	{

	}
	void input()
	{
		cin >> numerator >> denominator;
	}
	void output()
	{
		cout << numerator << "/" << denominator << endl;
	}
private:
	void reduce()
	{

	}
	int numerator;
	int denominator;
};

int main()
{
	fraction a, b;

	cout << "Enter a fraction: ";
	a.input();
	a.output();
	cout << "Enter a fraction: ";
	b.input();
	b.output();

	return 0;
}

Please try a little bit before asking for help. I would expect the plusEquals function to take a parameter of type 'fraction' for starters.

I'm not sure how to start it but I think the argument that the plusEquals function take is fraction second.

Please I need some help

Thank You!!

#include <iostream>
using namespace std;

class fraction
{
public:
	void plus_equals(fraction second)
	{
		
	}
	void minus_equals(fraction second)
	{

	}
	void times_equals(fraction second)
	{

	}
	void divide_equals(fraction second)
	{

	}
	double to_decimal()
	{

	}
	void input()
	{
		cin >> numerator >> denominator;
	}
	void output()
	{
		cout << numerator << "/" << denominator << endl;
	}
private:
	void reduce()
	{

	}
	int numerator;
	int denominator;
};

int main()
{
	fraction a, b;

	cout << "Enter a fraction: ";
	a.input();
	a.output();
	cout << "Enter a fraction: ";
	b.input();
	b.output();

	return 0;
}

You still have not tried. You can remove all of the other functions for the time being to make the code you post here much shorter. Also, instead of requiring user input, you should hardcode two fractions. Then give writing the plusEquals function a shot, and we can help if you get stuck.

The problem require to ask the user for the fractions and I have 1 question: How am I suppose to add 2 fractions together if they have the same variable names
That's the part I don't understand and there is no way for me to continue if I'm not getting plusEquals function part
I just need some hints or guide and after that I'm sure I can do the rest on my own

Thank You!!

No matter what the instructor has asked you to do for the final version, you can save yourself some trouble by hardcoding some values so you don't have to do the user input every time you run the program during the testing phase.

The variables will not have the same name:

fraction fraction1(n1,d1);
fraction fraction2(n2,d2);
fraction1.plusEquals(fraction2)

...

void fraction::plusEquals(fraction f)
{
 // add f to the current fraction here
}

The first question you need to answer is "How do I add a fraction?". Once you answer that, you know what you need to do in plusEquals. Take that same basic question and apply it to the other methods.

>>The problem require to ask the user for the fractions
Yes it does, but that doesn't meen that it has to immediately. The instructor isn't going to care how you got to your final result as long as it meets the requirements and you didn't cheat.

Make your life easier, do as David says. Write fraction::input() so that it has values hard-coded, then when you are ready, convert it to prompts. That's the real power and beauty of OOP, as long as you don't change the interface (the object's functions' names and return values), you can change the implementations (the bodies of the functions) as much as you want to without breaking the rest of your code.

What is source code? Text... It's easy to edit text.

class fraction {
 public:
   void input();
   int numerator;
   int denominator;
};

int main() {
  fraction f1;
  f1.input();
  return 0;
}

void fraction::input() {
 this->numerator = 5;
 this->denominator = 10;
}

Then, when you're ready to start accepting user input, change the definition of fraction::input().

void fraction::input() {
  int temp;
  cout << "Please enter numerator: ";
  cin >> numerator;
  do {
    cout >> "Please enter denominator: ";
    cin >> temp;
  } while (temp == 0);
  denominator = temp;
}

That's all you have to do. Your main() isn't going to care whether fraction::input() has been implemented one way or the other. All that it cares about is that it can successfully call it.

Edited 6 Years Ago by Fbody: n/a

I'm still not understanding this problem
How can I get two fraction into the void input() function and then call the void plus_equal function to add them up?

This is what I have and it's somewhat the same as what I have in my first post
I hardcoded some value for num and denom but when I run frac2.input and frac2.ouput they both just have the same value as the frac1.
How can I have 2 seperated fractions?

#include <iostream>
using namespace std;

class fraction
{
public:
	void plus_equal()
	{

	}
	void input()
	{
		num=2;
		denom=3;
	}
	void output()
	{
		cout << num << "/" << denom << endl;
	}
private:
	int num;
	int denom;
};

int main()
{
	fraction frac1, frac2;

	frac1.input();
	frac1.output();
	frac2.input();
	frac2.output();

	return 0;
}

Why would you expect them to be different? The constructor is the same, so of course the two objects will have the same value if you don't modify them.

fraction frac1, frac2;

	frac1.Set(n1,n2);
	frac1.output();
	frac2.input();
	frac2.output();

Of course you will need to write the Set(int,int) function.

You don't input 2 fractions in the input function. You have 2 fraction objects, each with their own input function. Your main() is correct.

After the fractions are input, you then use the first fraction object to call plusEquals using the second fraction as the argument to the call fraction1.plusEquals(fraction2);

Now I'm having some trouble figuring how to write up the adding fraction equation in the plus_equal function.
I know how to add them: num = ((num1 * denom2) + (num2 * denom1)) and the denom = (denom1 * denom2)
This is what I have now but kinda changed the hard-coded values for num and denom to ask user for the values cus I was kinda confusing myself
Thank You!!

#include <iostream>
using namespace std;

class fraction
{
public:
	void plus_equal(fraction frac2)
	{
		
	}
	void input()
	{
		cin >> num >> denom;
	}
	void output()
	{
		cout << num << "/" << denom << endl;
	}
private:
	int num;
	int denom;
};

int main()
{
	fraction frac1, frac2;

	cout << "Enter a fraction: ";
	frac1.input();
	frac1.output();
	cout << "Enter a fraction: ";
	frac2.input();
	frac2.output();
	frac1.plus_equal(frac2);

	return 0;
}
This article has been dead for over six months. Start a new discussion instead.