ok i cam writing a class named fraction to add subtract multiply and divide fractions and it also compares them. This is my first time writing a class and also the operators are suppose to be overloaded. I think i did them right. I just need to figure out why it wont work.

Here is my Fraction.h file

#pragma once
#include <iostream>
#include <fstream>
using namespace std;


class Fraction
{
	int num,denom;
public:
	Fraction( );
	Fraction(int num,int denom);
	void setfraction(int num,int denom);
	double getnumerator() const;
	double getdenominator() const;
	void print() const;
	Fraction operator+(const Fraction & fract) const;
    Fraction operator-(const Fraction & fract) const;
    Fraction operator*(const Fraction & fract) const;
    Fraction operator/(const Fraction & fract) const;
    bool operator==(const Fraction & fract) const;
    bool operator!=(const Fraction & fract) const;
    bool operator> (const Fraction & fract) const;
    bool operator>=(const Fraction & fract) const;
    bool operator< (const Fraction & fract) const;
    bool operator<=(const Fraction & fract) const;
	
private:
		int numerator, denominator;
  };

Here is my Fraction.cpp file

#include "StdAfx.h"
#include "Fraction.h"

Fraction::Fraction()
   {
   }

  Fraction::Fraction(int num,int denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  void Fraction::setfraction(int num,int denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  Fraction Fraction::operator+(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.num=num+fract.num;
	  tempFract.denom=denom+fract.denom;

	  return tempFract;
  }

 Fraction Fraction::operator-(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.num=num-fract.num;
	  tempFract.denom=denom-fract.denom;

	  return tempFract;
  }

 Fraction Fraction::operator*(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.num=num*fract.num;
	  tempFract.denom=denom*fract.denom;

	  return tempFract;
  }

 Fraction Fraction::operator/(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.num=num*fract.denom;
	  tempFract.denom=denom*fract.num;

	  return tempFract;
  }

  bool Fraction::operator==(const Fraction & fract) const
  {
	return (num==fract.num)&&(denom==fract.denom);
  }

 bool Fraction::operator!=(const Fraction & fract) const
  {
	return (num!=fract.num)||(denom!=fract.denom);
  }

 bool Fraction::operator> (const Fraction & fract) const

  {
	return (num> fract.num)&&(denom> fract.denom);
  }

 bool Fraction::operator>=(const Fraction & fract) const
  {
	return (num>=fract.num)&&(denom>=fract.denom);
  }

 bool Fraction::operator< (const Fraction & fract) const
  {
	return (num< fract.num)&&(denom< fract.denom);
  }

 bool Fraction::operator<=(const Fraction & fract) const
  {
	return (num<=fract.num)&&(denom<=fract.denom);
  }

 void Fraction::print() const
 {
	 cout<<numerator<<"/"<<denominator;
 }

Here is my main File it simple for now just one addition and multiplication but i cant get it to work.

#include "stdafx.h"
#include "Fraction.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	Fraction fract1(1,2);
	Fraction fract2(1,4);
	Fraction fract3;
	Fraction fract4;

	cout<<"fract1: ";
	fract1.print();
	cout<<endl;

	cout<<"fract2: ";
		fract2.print();
	cout<<endl;
	
fract3=fract1 + fract2;

cout<<"fract3: ";
  fract3.print();
  cout<<endl;

  fract4=fract1 * fract2;

  cout<<"fract4: ";
  fract4.print();

  cout<<endl;


	return 0;
}

whats the error your getting?

Assuming the code compiles none of it will produce the correct results because you have not correctly implemented fractional arithmetic. Take operator+

Fraction Fraction::operator+(const Fraction & fract) const
{
Fraction tempFract;
 
tempFract.num=num+fract.num;
tempFract.denom=denom+fract.denom;
 
return tempFract;
}

According to this code then

1   1   1 + 1   2   1
- + - = ----- = - = -
2   2   2 + 2   4   2

Clearly wrong since half + half = 1.

Every single one of your operators suffer from a similar problem. Get a maths book or get on the web and look up the correct algorithms for fractional arithmetic.

There are other issues for instance

getnumerator and getdenominator are not implemented. Also they return double then the class seems to hold the values they return as int.

You might want a mehtod that returns the fraction as a double.

You might want a method, possibly private, that simplifies a fraction.

Your operator== tests for precisely equal fractions rather than equivalence. 1/2 and 2/4 are equivalent fractions, they both actually have the value 1/2 but your operator== would return false because it compares for exactly matching denominators and numerators.

You have no way to hold whole parts of an integer. How would your class hold the value 2 3/4 (two and three quarters).

Can I add that you have also got a numerator and num, denominator and dem in the class.
That is also an area of confusion.

i do understand the algorithms are wrong now that i look at it. Stupid mistake but at the same time it wont add properly. if i have 1/2 and 1/4 i want it to return 2/6 but all i get is garbage. I switched all the ints to double now. Also i will ever be using mixed numbers like 2 3/4 it will be just simple fractions.

building on what stuXYZ said; you are using two separate variables for the same purpose. In the operators, you are assigning and comparing values in num and denom, but are displaying numerator and denominator.

I'm assuming the reason you declared num and denom is because you use them as parameters in the functions? That is not necessary; the names you use for the parameters of the functions are just what the value being passed in will be called within the function.

If I said int x=3; then passed x into a function with a header of :
void func (int random) , then there would be a variable called random that you'd be able to use in the function func, and it's value would be 3. But you didn't need to declare random anywhere else besides the parameter list.
Here's an example:

#include <iostream>
using namespace std;

void func (int random);

int main()
{
     int x=3;
     //here I'm calling function func and passing in x.
     func(x);
     return0;
}

void func(int random)
{
//I am using random, and it will have the value of 3; but the only //other place it's mentioned in in the parameter above.
     if (random==3)
          cout<<"Life is good!\n";
     else
          cout<<"Life is still good!\n";
}

If you go back and look at your code, you'll notice that there are a few places you modified num and denom instead of numerator and denominator, then displayed numerator and denominator. if you fix that it should work; you don't need to declare num and denom at all.

Hope that helped :)

building on what stuXYZ said; you are using two separate variables for the same purpose. In the operators, you are assigning and comparing values in num and denom, but are displaying numerator and denominator.

I'm assuming the reason you declared num and denom is because you use them as parameters in the functions? That is not necessary; the names you use for the parameters of the functions are just what the value being passed in will be called within the function.

If I said int x=3; then passed x into a function with a header of :
void func (int random) , then there would be a variable called random that you'd be able to use in the function func, and it's value would be 3. But you didn't need to declare random anywhere else besides the parameter list.
Here's an example:

#include <iostream>
using namespace std;

void func (int random);

int main()
{
     int x=3;
     //here I'm calling function func and passing in x.
     func(x);
     return0;
}

void func(int random)
{
//I am using random, and it will have the value of 3; but the only //other place it's mentioned in in the parameter above.
     if (random==3)
          cout<<"Life is good!\n";
     else
          cout<<"Life is still good!\n";
}

If you go back and look at your code, you'll notice that there are a few places you modified num and denom instead of numerator and denominator, then displayed numerator and denominator. if you fix that it should work; you don't need to declare num and denom at all.

Hope that helped :)

OK i got it......

Fraction.h

#pragma once
#include <iostream>
#include <fstream>
using namespace std;


class Fraction
{
	
public:
	Fraction();
	Fraction(double num,double denom);
	void setfraction(double num,double denom);
	double getnumerator() const;
	double getdenominator() const;
	void print() const;
	Fraction operator+(const Fraction & fract) const;
    Fraction operator-(const Fraction & fract) const;
    Fraction operator*(const Fraction & fract) const;
    Fraction operator/(const Fraction & fract) const;
    bool operator==(const Fraction & fract) const;
    bool operator!=(const Fraction & fract) const;
    bool operator> (const Fraction & fract) const;
    bool operator>=(const Fraction & fract) const;
    bool operator< (const Fraction & fract) const;
    bool operator<=(const Fraction & fract) const;
	
private:
		double numerator, denominator;
  };

Fraction.cpp file

#include "StdAfx.h"
#include "Fraction.h"

Fraction::Fraction()
   {
   }

  Fraction::Fraction(double num,double denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  void Fraction::setfraction(double num,double denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  Fraction Fraction::operator+(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator+denominator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator-(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator-denominator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator*(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator/(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator;
	  tempFract.denominator=denominator*fract.numerator;

	  return tempFract;
  }

  bool Fraction::operator==(const Fraction & fract) const
  {
	return (numerator==fract.numerator)&&(denominator==fract.denominator);
  }

 bool Fraction::operator!=(const Fraction & fract) const
  {
	return (numerator!=fract.numerator)||(denominator!=fract.denominator);
  }

 bool Fraction::operator> (const Fraction & fract) const

  {
	return (numerator> fract.numerator)&&(denominator> fract.denominator);
  }

 bool Fraction::operator>=(const Fraction & fract) const
  {
	return (numerator>=fract.numerator)&&(denominator>=fract.denominator);
  }

 bool Fraction::operator< (const Fraction & fract) const
  {
	return (numerator< fract.numerator)&&(denominator< fract.denominator);
  }

 bool Fraction::operator<=(const Fraction & fract) const
  {
	return (numerator<=fract.numerator)&&(denominator<=fract.denominator);
  }

 void Fraction::print() const
 {
	 cout<<numerator<<"/"<<denominator;
 }

Main

#include "stdafx.h"
#include "Fraction.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	Fraction fract1(1,2);
	Fraction fract2(1,3);
	Fraction fract3;
	Fraction fract4;

	cout<<"fract1: ";
	fract1.print();
	cout<<endl;

	cout<<"fract2: ";
		fract2.print();
	cout<<endl;
	
fract3=fract1+fract2;

cout<<"fract3: ";
  fract3.print();
  cout<<endl;

  fract4=fract1 * fract2;

  cout<<"fract4: ";
  fract4.print();
  cout<<endl;
  if(fract1!=fract2)
	  cout<<"Not Equal"<<endl;
  else
	  cout<<"Equal"<<endl;


	return 0;
}

Not sure if all my comparisons will work. Addition subtraction multiplication and division work.

You should probably add something into the operators that will reduce the fractions to compare them...because according to your program, 1/2 and 2/4 are not equal, and 1/2 is less than 2/4.

If you know how to use the % operator, that would help you with this.

or, you could just actually divide them into doubles and compare them that way.

double numerator, denominator;

Personally I would have made these int and made the entire class use int unless you really want to be able to do 2.5 / 3.2 type fractions (may be you do I don't know). ints process more quickly and do not suffer from the randomness of approximation that doubles do.

That is for ints 1 + 1 == 2 always buf or doubles you may sometimes find that 1.0 + 1.0 = 1.99999999999999

double numerator, denominator;

Personally I would have made these int and made the entire class use int unless you really want to be able to do 2.5 / 3.2 type fractions (may be you do I don't know). ints process more quickly and do not suffer from the randomness of approximation that doubles do.

That is for ints 1 + 1 == 2 always buf or doubles you may sometimes find that 1.0 + 1.0 = 1.99999999999999

This makes sense i will switch everything back to int because i don't need double.

Ok guys i about have this figured out. The only thing i cant get is my comparison functions are giving the right value.

#
bool Fraction::operator> (const Fraction & fract) const
#
 
#
{
#
return (numerator> fract.numerator)&&(denominator> fract.denominator);
#
}
#
 
#
bool Fraction::operator>=(const Fraction & fract) const
#
{
#
return (numerator>=fract.numerator)&&(denominator>=fract.denominator);
#
}
#
 
#
bool Fraction::operator< (const Fraction & fract) const
#
{
#
return (numerator< fract.numerator)&&(denominator< fract.denominator);
#
}
#
 
#
bool Fraction::operator<=(const Fraction & fract) const
#
{
#
return (numerator<=fract.numerator)&&(denominator<=fract.denominator);
#
}

Here is how i am calling them in the main function.

else if(oper=='>')
	{if(fract1>fract2)
	    cout<<"True"<<endl;
	else
		cout<<"Flase"<<endl;
	}

    else if(oper=='<')
	{if(fract1<fract2)
	    cout<<"True"<<endl;
	else
		cout<<"Flase"<<endl;
	}
  
    else if(oper=='>'&&oper2=='=')
	{if(fract1>=fract2)
	    cout<<"True"<<endl;
	else
		cout<<"Flase"<<endl;
	}

     else if(oper=='<'&&oper2=='=')
	{if(fract1<=fract2)
	    cout<<"True"<<endl;
	else
		cout<<"Flase"<<endl;
	}

You can not compare fractions unless they have the same denominator. If they have the same denominator then you can simply compare the numerators.

You can not compare fractions unless they have the same denominator. If they have the same denominator then you can simply compare the numerators.

If they don't have the same denominator you can multiply denominator of the 2nd by numerator of the 1st and then denominator of the 1st by numerator of the 2nd. If denominator 2 times numerator 1 is larger than denominator 1 times numerator 2, then fraction 1 is the bigger of the two. A diagram:

(N1*D2)     (N2*D1) 
N1                N2
__         X      __             if  N1*D2 < N2*D1 => Frac1 < Frac2

D1                D2

If they don't have the same denominator you can multiply denominator of the 2nd by numerator of the 1st and then denominator of the 1st by numerator of the 2nd. If denominator 2 times numerator 1 is larger than denominator 1 times numerator 2, then fraction 1 is the bigger of the two.

Yes but mathematically that is the equivalent of calculating the numerators of both fractions if they expressed with a denominator of (denominator 1 x denominator 2) my statement holds under the implied assumption (or at least I implied it given normal fraction mathematics) that given any number of fractions you can always find a common denominator which they can all be expressed in.

Yes but mathematically that is the equivalent of calculating the numerators of both fractions if they expressed with a denominator of (denominator 1 x denominator 2) my statement holds under the implied assumption (or at least I implied it given normal fraction mathematics) that given any number of fractions you can always find a common denominator which they can all be expressed in.

Yeah, I get what you are saying. It makes sense. "Cannot compare fractions...same denominator" was the phrase that had thrown me in what you said. I would consider the cross multiplying to be a possible "comparison," is all. If I had read your post a second time before I posted I would have gotten what you were implying in the purest sense. Anyway, no big deal.

This is what i did it works except for the ones with the =. If i send it the fractions 1/2 and 1/2 it was say false to them being equal when it should show true.

bool Fraction::operator> (const Fraction & fract) const

  {   int tempnum1;
      int tempnum2;
	  tempnum1=numerator*fract.denominator;
      tempnum2=fract.numerator*denominator;
	return (tempnum1> tempnum2);
  }

 bool Fraction::operator>=(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	  tempnum1=numerator*fract.denominator;
      tempnum2=fract.numerator*denominator;
	return (tempnum1>tempnum2)||(tempnum1=tempnum2);
  }

 bool Fraction::operator< (const Fraction & fract) const
  {
	int tempnum1;
      int tempnum2;
	  tempnum1=numerator*fract.denominator;
      tempnum2=fract.numerator*denominator;
	return (tempnum1< tempnum2);
  }

 bool Fraction::operator<=(const Fraction & fract) const
  {
	int tempnum1;
      int tempnum2;
	  tempnum1=numerator*fract.denominator;
      tempnum2=fract.numerator*denominator;
	return (tempnum1< tempnum2)||(tempnum1==tempnum2);
  }

If you have a working == and a working > then >=, < and <= can be defined in terms of the other 2 and you have less code to maintain.

However line 16 single = and why not use >=?
Line 34 why not use <=?

Apart from that I see good reason why they aren't working so try debugging with some well placed cout statements

..

i changed lines 16 and 34 to

return (tempnum1>=tempnum2);

return (tempnum1<=tempnum2);

not sure why this isnt working.
This is where it is called.

else if(oper=='>'&&oper2=='=')
	{if(fract1>=fract2)
	    cout<<"True"<<endl;
	else
		cout<<"Flase"<<endl;
	}

     else if(oper=='<'&&oper2=='=')
	{if(fract1<=fract2)
	    cout<<"True"<<endl;
	else
		cout<<"Flase"<<endl;

When I said well placed cout statements I meant cout statements inside the operators printing out all the values involved in the calculation.

Ok i have everything working except this one problem. I am want to print to an output file. On line 119 i changed from cout to outfile and get an error. It worked great with cout.

Fraction.cpp file

#include "StdAfx.h"
#include "Fraction.h"
#include <fstream>

 

Fraction::Fraction()
   {
   }

  Fraction::Fraction(int num,int denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  void Fraction::setfraction(int num,int denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  Fraction Fraction::operator+(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator+denominator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator-(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator-denominator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator*(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator/(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator;
	  tempFract.denominator=denominator*fract.numerator;

	  return tempFract;
  }

  bool Fraction::operator==(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1==tempnum2);
  }

 bool Fraction::operator!=(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1!=tempnum2);
  }

 bool Fraction::operator> (const Fraction & fract) const

  {   int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1> tempnum2);
  }

 bool Fraction::operator>=(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1>=tempnum2);
  }

 bool Fraction::operator< (const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1< tempnum2);
  }

 bool Fraction::operator<=(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1<=tempnum2);
  }

 void Fraction::print() const
 {
	outfile<<numerator<<"/"<<denominator;
 }

Where do you actually open outfile? Make sure it's open before you start to write to it.

Ok i feel stupid becasue i cant get my file to open globally and i keep getting this error.
Can you guys show me where i need to place the 4 statements

ifstream infile;
ofstream outfile;
infile.open("Input.txt");
outfile.open("Output.txt");

Here is my main

#include "stdafx.h"
#include "Fraction.h" .//MY outfile error is in this class
#include <iostream>
#include <fstream>
using namespace std;

ifstream infile;
ofstream outfile;





int _tmain(int argc, _TCHAR* argv[])
{ 
int top1,top2,bot1,bot2;
char oper,oper2,sym1,sym2,blank;
 
infile.open("Input.txt");       //OPEN INPUT FILE
outfile.open("Output.txt"); //OPEN OUTPUT FILE

 while(!infile.eof())
{
  infile>>top1;
  infile>>sym1;
  infile>>bot1;
  infile.get(blank);
  infile.get(oper);
  infile.get(oper2);
  infile>>top2;
  infile>>sym2;
  infile>>bot2;
  
	Fraction fract1(top1,bot1);//Builds fraction 1
	Fraction fract2(top2,bot2);//Builds fraction 2
	Fraction fract3;
	

	outfile<<"Fraction1 ";//Prints Fraction 1
	fract1.print();
	outfile<<endl;
	
    outfile<<"Fraction2 ";//Prints Fraction 2
		fract2.print();
	outfile<<endl;
   
	
	outfile<<"   "<<endl;
    fract1.print();
	outfile<<" "<<oper<<oper2<<" ";//Prints Operator
	fract2.print();
    outfile<<"= ";
	

	 if(oper=='+')// operator +
	   {fract3=fract1+fract2;
	    fract3.print();
	    outfile<<endl;}
	
	 else if(oper=='-')// operator -
	   {fract3=fract1-fract2;
	    fract3.print();
	    outfile<<endl;}

     else if(oper=='*')// operator *
	   {fract3=fract1*fract2;
        fract3.print();
	    outfile<<endl;}
	
	 else if(oper=='/')// operator /
	   {fract3=fract1/fract2;
	    fract3.print();
	    outfile<<endl;}

     else if(oper=='>')// operator >
	   {if(fract1>fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='<')// operator <
	   {if(fract1<fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }
  
     else if(oper=='>'&&oper2=='=')//operator >=
	   {if(fract1>=fract2)
	     outfile<<"True"<<endl;
        else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='<'&&oper2=='=')//operator <=
	   {if(fract1<=fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='!'&&oper2=='=')//operator !=
	   {if(fract1!=fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='='&&oper2=='=')// operator ==
	   {if(fract1==fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

       outfile<<"   "<<endl;// Blank line
       outfile<<"   "<<endl;// Blank line
}
	return 0;
}

Where you have them looks OK to me. What doesn't look OK is that you don't test to see if the files actually opened before trying to use them.

Where you have them looks OK to me. What doesn't look OK is that you don't test to see if the files actually opened before trying to use them.

What exactly do you mean?

Line 19 and 20 you open both files using open then at line 22 you use infile and at line 39 you use outfile.

At no point between calling open and using the objects do you test to make sure that the object is valid and the file is open.

Check your documentation of ifstream::open and ofstream::open for the failure cases.

Line 19 and 20 you open both files using open then at line 22 you use infile and at line 39 you use outfile.

At no point between calling open and using the objects do you test to make sure that the object is valid and the file is open.

Check your documentation of ifstream::open and ofstream::open for the failure cases.

I dont think i am having a problem with the files opening. I have a print function in my Fraction class and in the function i use outfile. Here is my error.

fraction.cpp(118) : error C2065: 'outfile' : undeclared identifier

I think i just need to open the files globally because its not open yet and the fucniton is trying to write to it. I tried opening globally and it didnt work. This is the last error i have with this program and really need to get it fixed.

Here is my error.

fraction.cpp(118) : error C2065: 'outfile' : undeclared identifier

If you had posted this instead of saying "this error" or said "this compiler error" the last 4 posts would not have been required. I thought you were dealing with a runtime error. Now do you see the importance of providing ALL the information you have.

Anyway, you are saying the error is in a piece of code that you have not posted? Does that really strike you as a good idea?

Is outfile declared in a place that is visible to Fraction.h? Remember in C++ that at any point you can only used what has already been seen in processing the file.

Anyway that is all beside the point, you should not be using global data, it completely breaks object encapsulation and is a maintenance nightmare. If infile is only used in main then declare it in main. In Fraction::print change it to accept a ostream& as a parameter and print into that.

If you had posted this instead of saying "this error" or said "this compiler error" the last 4 posts would not have been required. I thought you were dealing with a runtime error. Now do you see the importance of providing ALL the information you have.

Anyway, you are saying the error is in a piece of code that you have not posted? Does that really strike you as a good idea?

Is outfile declared in a place that is visible to Fraction.h? Remember in C++ that at any point you can only used what has already been seen in processing the file.

Anyway that is all beside the point, you should not be using global data, it completely breaks object encapsulation and is a maintenance nightmare. If infile is only used in main then declare it in main. In Fraction::print change it to accept a ostream& as a parameter and print into that.

Ok here is all my code.
MAIN

#include "stdafx.h"
#include "Fraction.h"
#include <iostream>
#include <fstream>
using namespace std;

ifstream infile;
ofstream outfile;





int _tmain(int argc, _TCHAR* argv[])
{ 
int top1,top2,bot1,bot2;
char oper,oper2,sym1,sym2,blank;
 
infile.open("Input.txt");
outfile.open("Output.txt");

 while(!infile.eof())
{
  infile>>top1;
  infile>>sym1;
  infile>>bot1;
  infile.get(blank);
  infile.get(oper);
  infile.get(oper2);
  infile>>top2;
  infile>>sym2;
  infile>>bot2;
  
	Fraction fract1(top1,bot1);//Builds fraction 1
	Fraction fract2(top2,bot2);//Builds fraction 2
	Fraction fract3;
	

	outfile<<"Fraction1 ";//Prints Fraction 1
	fract1.print();
	outfile<<endl;
	
    outfile<<"Fraction2 ";//Prints Fraction 2
		fract2.print();
	outfile<<endl;
   
	
	outfile<<"   "<<endl;
    fract1.print();
	outfile<<" "<<oper<<oper2<<" ";//Prints Operator
	fract2.print();
    outfile<<"= ";
	

	 if(oper=='+')// operator +
	   {fract3=fract1+fract2;
	    fract3.print();
	    outfile<<endl;}
	
	 else if(oper=='-')// operator -
	   {fract3=fract1-fract2;
	    fract3.print();
	    outfile<<endl;}

     else if(oper=='*')// operator *
	   {fract3=fract1*fract2;
        fract3.print();
	    outfile<<endl;}
	
	 else if(oper=='/')// operator /
	   {fract3=fract1/fract2;
	    fract3.print();
	    outfile<<endl;}

     else if(oper=='>')// operator >
	   {if(fract1>fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='<')// operator <
	   {if(fract1<fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }
  
     else if(oper=='>'&&oper2=='=')//operator >=
	   {if(fract1>=fract2)
	     outfile<<"True"<<endl;
        else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='<'&&oper2=='=')//operator <=
	   {if(fract1<=fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='!'&&oper2=='=')//operator !=
	   {if(fract1!=fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

     else if(oper=='='&&oper2=='=')// operator ==
	   {if(fract1==fract2)
	     outfile<<"True"<<endl;
	    else
		 outfile<<"Flase"<<endl;
	   }

       outfile<<"   "<<endl;// Blank line
       outfile<<"   "<<endl;// Blank line
}
	return 0;
}

FRACTION.h

#pragma once
#include <iostream>
#include <fstream>
using namespace std;


class Fraction
{
	

public:
	Fraction();
	Fraction(int num,int denom);
	void setfraction(int num,int denom);
	int getnumerator() const;
	int getdenominator() const;
	void print() const;
	Fraction operator+(const Fraction & fract) const;
    Fraction operator-(const Fraction & fract) const;
    Fraction operator*(const Fraction & fract) const;
    Fraction operator/(const Fraction & fract) const;
    bool operator==(const Fraction & fract) const;
    bool operator!=(const Fraction & fract) const;
    bool operator> (const Fraction & fract) const;
    bool operator>=(const Fraction & fract) const;
    bool operator< (const Fraction & fract) const;
    bool operator<=(const Fraction & fract) const;
	
private:
		int numerator, denominator;
  };

FRACTION.CPP MY error is on the very last line of code. In the print fucntion where i outfile.

#include "StdAfx.h"
#include "Fraction.h"
#include <fstream>


Fraction::Fraction()
   {
   }

  Fraction::Fraction(int num,int denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  void Fraction::setfraction(int num,int denom)
  {
	  numerator=num;
	  denominator=denom;
  }

  Fraction Fraction::operator+(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator+denominator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator-(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator-denominator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator*(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.numerator;
	  tempFract.denominator=denominator*fract.denominator;

	  return tempFract;
  }

 Fraction Fraction::operator/(const Fraction & fract) const
  {
	  Fraction tempFract;

	  tempFract.numerator=numerator*fract.denominator;
	  tempFract.denominator=denominator*fract.numerator;

	  return tempFract;
  }

  bool Fraction::operator==(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1==tempnum2);
  }

 bool Fraction::operator!=(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1!=tempnum2);
  }

 bool Fraction::operator> (const Fraction & fract) const

  {   int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1> tempnum2);
  }

 bool Fraction::operator>=(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1>=tempnum2);
  }

 bool Fraction::operator< (const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1< tempnum2);
  }

 bool Fraction::operator<=(const Fraction & fract) const
  {
	  int tempnum1;
      int tempnum2;
	   tempnum1=numerator*fract.denominator;
       tempnum2=fract.numerator*denominator;
	return (tempnum1<=tempnum2);
  }

 void Fraction::print() const
 {
	outfile<<numerator<<"/"<<denominator;
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.