6 Replies - 441 Views - Last Post: 21 June 2012 - 08:50 AM Rate Topic: -----

#1 VTMEC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 12

How do I receive a value from the driver file into a header file?

Posted 20 June 2012 - 08:56 PM

I have to use a user input value of "power" from the driver file in my operator ^ function in my header file and I can't figure out how to do it. Here is my code:

#include "hw11.h"

int main()
{
	
	Fraction fract_1, fract_2;
	cin >> fract_1 >> fract_2; 

	int power;	
	cout << "Enter a positive integer power to raise the fraction to >>> ";
	cin >> power;
	cout << endl;
	cout << "The entered fraction, reduced is "<< fract_1 << endl 
		<< "The entered fraction, reduced is " << fract_2 << endl << endl;
	
	// addition
	Fraction fract_3 = fract_1 + fract_2;
	cout << "fraction 1 + fraction 2 = " << fract_3 << endl << endl;

	// subtraction
	fract_3 = fract_1 - fract_2;
	cout << "fraction 1 - fraction 2 = " << fract_3 << endl << endl;


	// multiplication
	fract_3 = fract_1 * fract_2;
	cout << "fraction 1 * fraction 2 = " << fract_3 << endl << endl;


	// division
	fract_3 = fract_1 / fract_2;
	cout << "fraction 1 / fraction 2 = " << fract_3 << endl << endl;

	// power
	fract_3 = fract_1 ^ power;
	cout << "fraction 1 ^ " << power << " = " << fract_3 << endl << endl;

	return 0;
}


Here is the header file that must use the input of power:

#include<iostream>
using namespace std;


class Fraction
{
private:
	int numerator1, denominator1;
public:
	Fraction(){numerator1 = -99999; denominator1 = -99999;}
	int getNumerator1() const {return numerator1;}
	int getDenominator1() const {return denominator1;}
	void setNumerator1(int N) {numerator1 = N;}
	void setDenominator1(int D) {denominator1 = D;}
	int Reduce();
	int Sign;
	int numerator;
	int power;
	Fraction operator+ (Fraction&);
	Fraction operator- (Fraction&);
	Fraction operator* (Fraction&);
	Fraction operator/ (Fraction&);
	Fraction operator^ (int power);
friend istream& operator >> (istream&, Fraction&);
friend ostream& operator << (ostream&, Fraction&);
};

istream& operator >> (istream& in, Fraction& F)
{
	int FirstNum = 0;
	int SecondNum = 0;
	char thing;
	cout << "Enter a fraction in the form \"numerator/denominator\" >>> ";
	in >> FirstNum >> thing >> SecondNum;
	F.numerator1 = FirstNum;
	F.denominator1 = SecondNum;
	return in;
}

ostream& operator << (ostream& out, Fraction& F)
{
	F.Reduce();
	int count = 0;
	if(F.numerator > F.denominator1)
	{
		int remainder = F.numerator%F.denominator1;
	while(F.numerator >= F.denominator1)
	{
		count++;
		F.numerator -= F.denominator1;
		
	}
	out << F.Sign * count << " " << remainder << "/" << F.denominator1;
	}
	else
	{
		out << F.numerator1 << "/" << F.denominator1;
	}
	if(F.numerator1 == F.denominator1)
	{
		out << 1;
	}
	return out;
}

int Fraction::Reduce()
{
	Sign = 1;
	if (numerator1 == 0 || denominator1 == 0)
	{
		denominator1 = 1;
		return 0;
	}
	else
	{

		if(numerator1 < 0)
		{
			Sign = -1;
			numerator1 =  -1 * numerator1;
		}
		if(denominator1 < 0)
		{
			Sign = -1;
			denominator1 = -1 * denominator1;
		}
	}
	int GCD = numerator1;
	int Dum = denominator1;
	while(GCD != Dum)
	{
		if(GCD > Dum)
		{
			GCD = GCD - Dum;
		}
		if(Dum > GCD)
		{
			Dum = Dum - GCD;
		}
		if(GCD == Dum)
		{
			break;
		}
	}
	numerator = (numerator1/GCD);
	numerator1 = Sign * (numerator1/GCD);
	denominator1 = denominator1/GCD;
	return numerator, numerator1, denominator1;
}

Fraction Fraction::operator+ (Fraction& F)
{
	Fraction Add;
	Add.numerator1 = numerator1 * F.denominator1 + F.numerator1 * denominator1;
	Add.denominator1 = denominator1 * F.denominator1;
	return Add;
}

Fraction Fraction::operator- (Fraction& F)
{
	Fraction Sub;
	Sub.numerator1 = numerator1 * F.denominator1 - F.numerator1 * denominator1;
	Sub.denominator1 = denominator1 * F.denominator1;
	return Sub;
}

Fraction Fraction::operator* (Fraction& F)
{
	Fraction Mult;
	Mult.numerator1 = numerator1 * F.numerator1;
	Mult.denominator1 = denominator1 * F.denominator1;
	return Mult;
}

Fraction Fraction::operator/ (Fraction& F)
{
	Fraction Div;
	Div.numerator1 = numerator1 * F.denominator1;
	Div.denominator1 = denominator1 * F.numerator1;
	return Div;
}

Fraction Fraction::operator ^ (int power)
{
	Fraction Pow;
	for(int i = 1; i = power; i++)
	{
	Pow.numerator1 = numerator1 * numerator1;
	Pow.denominator1 = denominator1 * denominator1;
	if(i = power)
	{
		break;
	}
	}
	return Pow;
}



If I run what I have now, it only gives me the fraction to the second power no matter what power I input. What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: How do I receive a value from the driver file into a header file?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,426
  • Joined: 25-December 09

Re: How do I receive a value from the driver file into a header file?

Posted 20 June 2012 - 09:07 PM

You may want to insure your compiler is generating warnings. Here are some warnings I received when I compiled your code:

Quote

main.cpp||In member function ‘int Fraction::Reduce()’:|
main.cpp|133|warning: left operand of comma operator has no effect [-Wunused-value]|
main.cpp|133|warning: right operand of comma operator has no effect [-Wunused-value]|
main.cpp||In member function ‘Fraction Fraction::operator^(int)’:|
main.cpp|168|warning: declaration of ‘power’ shadows a member of 'this' [-Wshadow]|
main.cpp|171|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
main.cpp|175|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|


Line 133 is:
	return numerator, numerator1, denominator1;

And 175 is:
	for(int i = 1; i = power; i++)

Remember operator= is assignment, you probably need comparison in the second part of this statement.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 VTMEC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 12

Re: How do I receive a value from the driver file into a header file?

Posted 20 June 2012 - 09:16 PM

My compiler gives no warnings when I run my program. I am using Visual Studio 2010. I change the equal sign to a less than but it is still doing the same thing. No matter what power I input, it just squares it and outputs it.
Was This Post Helpful? 0
  • +
  • -

#4 VTMEC++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 03-June 12

Re: How do I receive a value from the driver file into a header file?

Posted 20 June 2012 - 09:36 PM

Never mind, I figured it out. I had to use *this in my operator^ function to call the original object.
Was This Post Helpful? 0
  • +
  • -

#5 lolo1116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-June 12

Re: How do I receive a value from the driver file into a header file?

Posted 21 June 2012 - 08:09 AM

View PostVTMEC++, on 20 June 2012 - 09:36 PM, said:

Never mind, I figured it out. I had to use *this in my operator^ function to call the original object.


how did you use *this in your operator?
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1657
  • View blog
  • Posts: 3,135
  • Joined: 30-May 10

Re: How do I receive a value from the driver file into a header file?

Posted 21 June 2012 - 08:23 AM

@VTMEC++
Whilst you can override ^ to mean "raise to power of" (in place of bitwise exclusive-or), what you CANNOT do is change it's precedence.

So whilst in the normal mathematical sense, raising to the power of something has a precedence higher than multiplication, the precedence of exclusive-or is much much lower.
http://en.cppreferen...ator_precedence

So if you were to write in your code
if ( fract_1 ^ power > 10 )
you would be in for a nasty surprise, namely evaluating fract_1 ^ (power > 10)
Was This Post Helpful? 1
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How do I receive a value from the driver file into a header file?

Posted 21 June 2012 - 08:50 AM

Just a note about header files: Unless we are talking about templates -- In general one should not put executable code into a header file.

A header file (.h) is meant to contain declarations which tell the compiler how to use named objects like global/static variables, classes, and functions. They are also an nice place for preprocessor macros.

A .c/.cpp file contains the definitions i.e. the code for those functions, classes etc.

Now when linking (you do remember that the compiler compiles the source code into object files, which the linker then links together with libraries and other object files to form the executable) if you have the definitions included into multiple .cpp files (which are compiled into object files) then the functions/classes/etc. will exist in more than one place -- and the linker will be confused on weather you mean the Fraction::operator/() function from file1.obj or the one from file2.obj -- ahhh Linker ERROR!

So because C/C++ requires you to DECLARE something before you can use it, you put DECLARATIONS into a header file that can be #included in multiple files.

BUT you only DEFINE things in one file -- and then you link to that object file (or put it in a library). The header tells the compiler how to use the resources (classes/functions etc), and the object file/library links in the executable code.


So you would want to have:
hw11.h -- header file with declarations
hw11.cpp -- .cpp file with all of the definitions required by hw11.h (so this file #includes hw11.h)
main.cpp -- the main module which #includes hw11.h
someother.cpp -- some other file which may use hw11.h functions/classes

This post has been edited by NickDMax: 21 June 2012 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1