# Converting Fractions to Decimals and Vice Versa

Page 1 of 1

## 9 Replies - 1234 Views - Last Post: 05 August 2014 - 07:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=351431&amp;s=7a36a59efe8557fb8dd6913a70ff90d0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jynx678

Reputation: 0
• Posts: 23
• Joined: 01-August 14

# Converting Fractions to Decimals and Vice Versa

Posted 01 August 2014 - 01:05 PM

I'm working on a Fraction Class assignment where I'm supposed to provide conversion operators to allow Fraction objects to be used in expressions containing float data values. The way I have it now, I'm getting an error that says operator float(const Fraction &) must contain 'void', which makes no sense to me. Lines 155 - 173. Thank you in advance for your help.

```// Fractions CLASS
// Source: Fraction2012.cpp
// Author: Warren H. Knox, Jr.
//   Date: 11/8/2012
#include <iostream>
#include <cstdlib>
using namespace std;

class Fraction
{
private:
int WholeNumber;
int Numerator;
int Denominator;

public:
Fraction()
{
this->WholeNumber = 0;
this->Numerator = 0;
this->Denominator = 1;
}

Fraction(int w, int n, int d): WholeNumber(w), Numerator(n), Denominator(d)
{

}

Fraction(const Fraction & rhs)// Copy constructor
{
this->WholeNumber = rhs.WholeNumber;
this->Numerator = rhs.Numerator;
this->Denominator = rhs.Denominator;
}

// Accessor Functions
int getWholeNumber() const;
int getNumerator() const;
int getDenominator() const;

void getFraction() const
{
cout << this->WholeNumber;
if (this->Numerator)
cout << " " << this->Numerator
<< "/"
<< this->Denominator;
return;
}

void setFraction(int w, int n, int d)
{
this->WholeNumber = w;
this->Numerator = n;
this->Denominator = d;
} // YOU provide a definition

void setWhole(int w)
{
this->WholeNumber = w;
}
void setFraction()
{
char Temp[] = "                ";

char *pChr;

char w[8], n[8], d[8];

int count;
for(count=0;count < 7; count++)
{
w[count] = ' ';
n[count] = ' ';
d[count] = ' ';
}
d[count] = '\0';
n[count] = '\0';
w[count] = '\0';

pChr = &Temp[15];
cin.getline(Temp,15);

while (*pChr == ' ' && (pChr >= &Temp[0]))
{
pChr--;
}
*pChr = '\0';
int NullLocation = (pChr - Temp);

pChr--;

char *pDen = &d[6];
char *pNum = &n[6];
char *pWhole = &w[6];

for(count=0;count < NullLocation; count++)
{
if (Temp[count] == '/')
break;
}

if (count >= NullLocation)
{
this->Denominator = 1;
this->Numerator = 0;
} // End if
else
{
while (*pChr != '/')
{
*pDen = *pChr;
pDen--;
pChr--;
}
this->Denominator = atoi(d);

pChr--;
while (*pChr != ' ')
{
*pNum = *pChr;
pNum--;
pChr--;
}
this->Numerator = atoi(n);
} // End else

while (pChr >= &Temp[0])
{
*pWhole = *pChr;      // Store the digit
pWhole--;
pChr--;
}
this->WholeNumber = atoi(w);
.
return;
}

void ReduceFractions();
void SubFractions(Fraction & , Fraction & );

Fraction operator + (const Fraction &);
Fraction operator - (const Fraction & );
Fraction operator = (const Fraction & );
Fraction operator * (const Fraction & );
Fraction operator % (const Fraction & );

float operator + (float );

Fraction Convert2Fraction (float rhs)
{
Fraction temp;

temp.WholeNumber = static_cast<int>(rhs);
temp.Numerator = rhs  * 1000;
temp.Denominator = 1000;
ReduceFractions();

return temp;
}
operator float(const Fraction & rhs)
{
Fraction lhs;
float decimal;
lhs.Numerator = (rhs.WholeNumber * rhs.Denominator) + rhs.Numerator;
decimal = static_cast<float>(lhs.Numerator) / rhs.Denominator;
return decimal;
}

};

void Fraction::AddFractions(Fraction & One, Fraction & Two)
{
int New1;
int New2;
int NewNumerator;
int CarryOut;

this->WholeNumber = One.WholeNumber + Two.WholeNumber;
this->Denominator = One.Denominator * Two.Denominator;
New1 = One.Numerator * Two.Denominator;
New2 = Two.Numerator * One.Denominator;
NewNumerator = New1 + New2;
CarryOut = NewNumerator / this->Denominator;
this->WholeNumber += CarryOut;
this->Numerator = NewNumerator % this->Denominator;
return;
}
void Fraction::SubFractions(Fraction & One, Fraction & Two)
{
int New1;
int New2;
int NewNumerator;
int CarryOut;

this->WholeNumber = One.WholeNumber - Two.WholeNumber;
this->Denominator = One.Denominator * Two.Denominator;
New1 = One.Numerator * Two.Denominator;
New2 = Two.Numerator * One.Denominator;
NewNumerator = New1 - New2;
CarryOut = NewNumerator / this->Denominator;
this->WholeNumber -= CarryOut;
this->Numerator = NewNumerator % this->Denominator;
return;
}

void Fraction::ReduceFractions()
{

int First = this->Numerator;
int Second = this->Denominator;
int Temp;

if(this->Numerator == 0)         // Check for 0 numerator
{
this->Denominator = 1;
return;
}

// Find greatest common denominator
while (First != Second)
{
if (First < Second)           // First is not larger
{
Temp = First;                  // Swap values
First = Second;
Second = Temp;
}
First -= Second;
} // End while

this->Numerator /= First;
this->Denominator /= First;
return;
}
Fraction Fraction::operator + (const Fraction & rhs)
{
int New1;
int New2;
int NewNumerator;
int CarryOut;
Fraction Sum;

Sum.WholeNumber = WholeNumber + rhs.WholeNumber;
Sum.Denominator = Denominator * rhs.Denominator;
New1 = Numerator * rhs.Denominator;
New2 = rhs.Numerator * Denominator;
NewNumerator = New1 + New2;
CarryOut = NewNumerator / Sum.Denominator;
Sum.WholeNumber += CarryOut;
Sum.Numerator = NewNumerator % Sum.Denominator;
return Sum;
}
float Fraction::operator + (float rhs)
{
float lhs;
float sum = lhs + rhs;
}
Fraction Fraction::operator - (const Fraction & rhs)
{
int New1;
int New2;
int NewNumerator;
int CarryOut;
Fraction Difference;

Difference.WholeNumber = WholeNumber - rhs.WholeNumber;
Difference.Denominator = Denominator * rhs.Denominator;
New1 = Numerator * rhs.Denominator;
New2 = rhs.Numerator * Denominator;
NewNumerator = New1 - New2;
CarryOut = NewNumerator / Difference.Denominator;
Difference.WholeNumber -= CarryOut;
Difference.Numerator = NewNumerator % Difference.Denominator;
return Difference;
}
Fraction Fraction::operator * (const Fraction & rhs)
{

int NewNumerator;
int tempNumerator;
int CarryOut;
int rhsNum;
Fraction Product;

tempNumerator = (WholeNumber * Denominator) + Numerator;
rhsNum = rhs.WholeNumber * Denominator;
NewNumerator = tempNumerator * rhsNum;
Product.Denominator = Denominator * Denominator;
CarryOut = NewNumerator / Product.Denominator;
Product.WholeNumber += CarryOut;
Product.Numerator = NewNumerator % Product.Denominator;
return Product;
}
Fraction Fraction::operator % (const Fraction & rhs)
{
int NewNumerator;
int tempNumeratorOne;
int tempNumeratorTwo;
int tempDenominator;
int CarryOut;
Fraction Quotient;

tempNumeratorOne = (WholeNumber * Denominator) + Numerator;
tempNumeratorTwo = (rhs.WholeNumber * rhs.Denominator) + rhs.Numerator;
tempDenominator = tempNumeratorTwo;
NewNumerator = rhs.Denominator;
Quotient.Numerator = tempNumeratorOne * NewNumerator;
Quotient.Denominator = Denominator * tempDenominator;
CarryOut = Quotient.Numerator / Quotient.Denominator;
Quotient.WholeNumber += CarryOut;
Quotient.Numerator = Quotient.Numerator % Quotient.Denominator;
return Quotient;

}
Fraction Fraction::operator =(const Fraction & rhs)
{
Fraction One;

One.WholeNumber = rhs.WholeNumber;
One.Numerator = rhs.Numerator;
One.Denominator = rhs.Denominator;
return One;
}

int main()
{
Fraction FirstOne;               // A fraction value
Fraction SecondOne;              // Another fraction value
float fOne = 3.6f;
float temp;

Fraction NewSum = AddOne + fOne;

cout << endl << "The sum of ";
cout << " and ";
fOne;
cout << " is ";
NewSum.getFraction();
cout << endl << endl;

FirstOne.setFraction(5,2,5);
SecondOne.setFraction(2,1,4);

Fraction Difference = FirstOne - SecondOne;
Difference.ReduceFractions();

cout << endl << "The difference of ";
FirstOne.getFraction();
cout << " and ";
SecondOne.getFraction();
cout << " is ";
Difference.getFraction();
cout << endl << endl;

NewOne.setFraction(2,1,2);
NewTwo.setWhole(3);
Fraction NewProduct = NewOne * NewTwo;
NewProduct.ReduceFractions();

cout << endl << "The difference of ";
NewOne.getFraction();
cout << " and ";
NewTwo.getFraction();
cout << " is ";
NewProduct.getFraction();
cout << endl << endl;

New3.setFraction(12,1,4);
New4.setFraction(2,1,2);

Fraction Quotient = New3 % New4;
Quotient.ReduceFractions();

cout << endl << "The Quotient of ";
New3.getFraction();
cout << " and ";
New4.getFraction();
cout << " is ";
Quotient.getFraction();
cout << endl << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Converting Fractions to Decimals and Vice Versa

### #2 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Converting Fractions to Decimals and Vice Versa

Posted 01 August 2014 - 03:02 PM

Well you don't usually take a parameter on a float operator overload because the float operator overload is called when the current object needs to be converted itself to a float. Fix this up like this...

```operator float() const
{
return (float)this->Numerator / (float)this->Denominator;
}

```

Here we are saying when requested to become a float, return this object's numerator divided by its denominator (could probably check to make sure it is not zero while you are at it) and return this value as a float. The function should also probably be a const.

### #3 jynx678

Reputation: 0
• Posts: 23
• Joined: 01-August 14

## Re: Converting Fractions to Decimals and Vice Versa

Posted 01 August 2014 - 03:06 PM

Thank you so much. I really appreciate it.

### #4 jynx678

Reputation: 0
• Posts: 23
• Joined: 01-August 14

## Re: Converting Fractions to Decimals and Vice Versa

Posted 01 August 2014 - 03:14 PM

One last question, what is the appropriate syntax when calling the function in main, say around line 341?

### #5 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Converting Fractions to Decimals and Vice Versa

Posted 01 August 2014 - 03:33 PM

It is a conversion operator, so it is when it is casted to a float...

```   Fraction f;
f.setFraction(1,3,4);

cout << (float)f << endl;

```

Here the casting to float (done explicitly here) will cause that operator to kick off and convert the values in "f" to a decimal (takes 3 / 4 and returns .75) to be used in the cout line.

This post has been edited by Martyr2: 01 August 2014 - 03:35 PM

### #6 jynx678

Reputation: 0
• Posts: 23
• Joined: 01-August 14

## Re: Converting Fractions to Decimals and Vice Versa

Posted 01 August 2014 - 03:34 PM

Nevermind I got it!

### #7 jynx678

Reputation: 0
• Posts: 23
• Joined: 01-August 14

## Re: Converting Fractions to Decimals and Vice Versa

Posted 01 August 2014 - 07:41 PM

The Fraction to float function works, but I'm still having trouble with float to Fraction. I wrote it similarly to the other function, but the call does not work. I get the error 'No matching function for call to 'Fraction::Fraction(float &)'' which tells me the wrong function is being called.
This is the function
```  operator Fraction()
{
Fraction temp;

temp.WholeNumber = WholeNumber;
temp.Numerator = Numerator  * 1000;
temp.Denominator = 1000;

return temp;
}
```

This is the call
```float assnvar = 2.5f;
Fraction assignment;
assignment = static_cast<Fraction>(assnvar);
assignment.ReduceFractions();
cout<<assignment;
```

Thanks again

### #8 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: Converting Fractions to Decimals and Vice Versa

Posted 03 August 2014 - 06:40 AM

There's no need for an opeator Fraction() function because the object is already a fraction.

### #9 jynx678

Reputation: 0
• Posts: 23
• Joined: 01-August 14

## Re: Converting Fractions to Decimals and Vice Versa

Posted 03 August 2014 - 09:49 AM

Even if I'm converting a float back to a fraction? What would an appropriate call be?

### #10 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: Converting Fractions to Decimals and Vice Versa

Posted 05 August 2014 - 07:25 AM

That would be a constructor call or just a simple member function that takes a float. Remember that floating point arithmetic on computers is at-best an approximation, so depending on the algorithm you use to convert a float to a fraction, you might not end up with the correct result.

For example, the simple fractions like 1/2 or 1/4 will be easy to convert, it's the fractions like 1/3 that you might have a problem with.

jynx678, on 03 August 2014 - 12:49 PM, said:

Even if I'm converting a float back to a fraction? What would an appropriate call be?

The operator Fraction() function would have to be defined for the float type and not the Fraction type like you have. You can't modify the POD types anyways and that's why I say a constructor, assignment, or member function taking a float will work.