HOw to Reduce Fractions and How to ovrload the >> opertor in classes???

SO, This is my code:

This is the H.File

#ifndef H_fractionType
#define H_fractionType

# include <iostream>

using namespace std;

class fractionType
{
friend ostream& operator<< (ostream&, const fractionType&);
friend istream& operator>> (istream&, const fractionType&);

public:
void setFraction (const double& x, const double& y, const double& z, const double& r);
//Function to set the member variables x, y, z.
//Postcondition: a=x, b=y, c=z.

void getFraction (double& x, double& y, double& z, double& r) const;
//Function to return fraction.
//Postcondition: x=a, y=b, z=c.

bool operator==(const fractionType& otherFraction) const;
//overload the equality operator.
//Postcondition: Returns true if the fraction
//is equal to the ohter fraction, otherwise it returns false.

bool operator!= (const fractionType& otherFraction) const;
//Overload the not equal operator.
//Postcondition: REturns true if the fraction is not
// equal to the other fraction, otherwise it returns false.


fractionType operator+ (const fractionType& otherFraction) const;
//Overload the addition operator.


fractionType operator- (const fractionType& otherFraction) const;
//Overload subtraction operator.


fractionType operator* (const fractionType& otherFraction) const;
//overloa the multiplication operator.


fractionType operator/ (const fractionType& otherFraction) const;
//Overload the division operator.


fractionType( double x = 0, double y = 0, double z = 0, double r = 0);
//Constructor to initialize the object with the values
//specified by the user. If no values are specified, the
//default values are assumed.
//Postcondition: a=x, b=y, c=z.

private:
double a; //variable to store x
double b; //variable to store y
double c; //variable to store z
double d; //Variable to store r

};
#endif

This is the Imp file:

# include <iostream>
# include <cmath>
# include <string>
#include "fractionType.h"

using namespace std;

ostream& operator<< (ostream& osObject, const fractionType& fraction)
{
osObject << " ";
osObject << fraction.a;
osObject << " / ";
osObject << fraction.b;
osObject <<"";

return osObject; //Returns the ostream object
}

istream& operator>> (istream& isObject, const fractionType& fraction)
{
char ch;

isObject.get (ch);
isObject >> fraction.a;
isObject.get (ch);
isObject >> fraction.b;
isObject.get (ch);
isObject >> fraction.c;
isObject.get (ch);
isObject >> fraction.d;
isObject.get (ch);

return isObject; //Return the istream object
}

//Constructor
fractionType::fractionType (double x, double y, double z, double r)
{
a = x;
b = y;
c = z;
d = r;
}

void fractionType::setFraction (const double& x, const double& y, const double& z, const double& r)
{
a = x;
b = y;
c = z;
d = r;

}

void fractionType::getFraction (double& x, double& y, double& z, double& r) const
{
x = a;
y = b;
z = c;
r = d;
}

bool fractionType::operator== (const fractionType& otherFraction) const
{
return (a == otherFraction.a && b == otherFraction.b &&
c == otherFraction.c && d == otherFraction.d);
}

bool fractionType::operator!= (const fractionType& otherFraction) const
{
return (a != otherFraction.a || b != otherFraction.b ||
c != otherFraction.c || d != otherFraction.d);
}

fractionType fractionType::operator* (const fractionType& otherFraction) const
{
fractionType temp;

temp.a = a * otherFraction.a;
temp.b = b * otherFraction.b;

return temp;
}


fractionType fractionType::operator+ (const fractionType& otherFraction) const
{
fractionType temp;

temp.a = ((a * otherFraction.b) + (b * otherFraction.a));
temp.b = b * otherFraction.b;


return temp;
}

fractionType fractionType::operator- (const fractionType& otherFraction) const
{
fractionType temp;

temp.a = ((a * otherFraction.b) - (b * otherFraction.a));
temp.b = b * otherFraction.b;

return temp;
}

fractionType fractionType::operator/ (const fractionType& otherFraction) const
{
fractionType temp;

temp.a = a * otherFraction.b;
temp.b = b * otherFraction.a;

return temp;
}

This is my MAIN:

/* Author: Cesar Martinez
Date: April 17, 2011
Programing: Martinez Assig 5
Description: Overloading operators*/


# include <iostream>
# include <cmath>
# include <string>
# include <conio.h>
#include "fractionType.h"

using namespace std;

void personalinfo();
void header();
void farewell();

int main()
{

personalinfo();//My personal information
header();//The header of the assignment


fractionType fraction1;
fractionType fraction2;
fractionType fraction3;

cout << "\t\t\t\Welcome to Fraction Operations" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;

cout << "Please Enter Fraction 1 in form (x,y): ";
cin >> fraction1;
cout << endl;
cout << "" << endl;

cout << "Please Enter Fraction 2 in form (x,y): ";
cin >> fraction2;
cout << endl;
cout << "" << endl;

system ("pause");
system ("cls");
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;

cout << "Fraction 1 is: " << fraction1 << endl;
cout << "Fraction 2 is: " << fraction2 << endl;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;

cout << "\t\t\t\Adding Fractions" << endl;
fraction3 = fraction1 + fraction2;
cout << "Fraction 1 + Fraction 2 = " << endl;
cout << fraction3 << endl;
cout << "" << endl;

cout << "\t\t\t\Subtracting Fractions" << endl;
fraction3 = fraction1 - fraction2;
cout << "Fraction 1 - Fraction 2 = " << endl;
cout << fraction3 << endl;
cout << "" << endl;

cout << "\t\t\t\Multiplying Fractions" << endl;
fraction3 = fraction1 * fraction2;
cout << "Fraction 1 * Fraction 2 = " << endl;
cout << fraction3 << endl;
cout << "" << endl;

cout << "\t\t\t\Dividing Fractions" << endl;
fraction3 = fraction1 / fraction2;
cout << "Fraction 1 / Fraction 2 = " << endl;
cout << fraction3 << endl;
cout << "" << endl;

system ("pause");
system ("cls");

//Funtion call for goodbye message
farewell();

system ("pause");
system ("cls");
return 0;
}

I can't have the >> operator to work. When it asks me for the first fraction, I input the data in the form (1,3), but It just stays still. The Enter button doesnt do anything. It just allows me to write whatever I want but it never takes me to the next fraction.

If I do

fraction1 (1,3);
fraction2 (1,3);
fraction3;

fraction3 = fraction1 + fraction2;

It works!!!
It works fine, but when I ask the user to input the data, it doesnt do anything.

Any ideas?? Or what am I missing???

Also, how do reduce the fractions?? and where do I write the code???


PLEASE HELP
No I'm not going to read all that unformatted code not in [code] tags. Well, just a little bit.. Why in the world does your fraction have 4 doubles, when is should have 2 integers?

Reduction:
fraction F = {a, b};
int g = gcd(a, b) where gcd stands for greatest common divisor
reduced fraction F' = {a/g, b/g}
If you don't know how to write a gcd function, google it. There is pseudocode in wikipedia.

>>:
you want to read "12/13" into {12, 13}, so (you may first want to ignore any whitespaces with stream >> ws;) read a, ignore one char, read b.
Thanks for the reduction, I will work on it.

Now as far for the >> operator

istream& operator>> (istream& isObject, const fractionType& fraction)
{
char ch;

isObject.get (ch); //Ignore (
isObject >> fraction.a;
isObject.get (ch); // Ignore ,
isObject >> fraction.b;
isObject.get (ch); //Ignore )

return isObject; //Return the istream object
}

But it just stays still. I'm ignoring the () and the comma, unless I'm doing it wrong. What drives me crazy is that I am following what the book says in order to get it right, but it doesnt work. It just stays still and it doesnt go to the next statement where I'm supposed to enter the next fraction.
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
#include <iostream>

struct Fraction{
	int a, b;
};

std::istream& operator >> (std::istream& i, Fraction& f){
	char ch;

	i.get (ch); //Ignore (
	i >> f.a;
	i.get (ch); // Ignore ,
	i >> f.b;
	i.get (ch); //Ignore )

	return i; //Return the istream object
}

int main(){
	Fraction f;
	std::cin >> f;
	std::cout << f.a << ' ' << f.b;
	std::cin.ignore().get();
	return 0;
}

Works fine for me..
Topic archived. No new replies allowed.