>What am I doing wrong
I told you what you're doing wrong. Now I'll spell it out for you. Everything in red is wrong.

Fraction operator+ (const Fraction& firval, const Fraction& secval)
{
	Fraction result;

	short mult = gcd( numerator, denominator);

	result.denominator = (denominator * secval.denominator) / mult;
	result.numerator = (numerator * secval.denominator) + (secval.numerator * denominator);

	result.reduce();

	return result;
}

Tell me where you're getting the values of numerator and denominator in the red parts.

DAMN :(

Fraction operator+ (const Fraction& firval, const Fraction& secval)
{
	Fraction result;

	short mult = gcd(firval.denominator, secval.denominator);

	result.denominator = (firval.denominator * secval.denominator) / mult;
	result.numerator = (firval.numerator * secval.denominator) + (secval.numerator * firval.denominator);

	result.reduce();

	return result;
}

I'm almost certain that gcd(firval.denominator, secval.denominator); is correct, because it's those two value's that I need to get the correct returnvalue.

Problem is, I'm still getting the same error, has it got to do with the definition of function gcd( short numerator, short denominator); ??????

I'm sorry Narue, I can't seem to find the correct solution :!:

>I'm still getting the same error
Same error as in compiler panic? Shut down your compiler and start it up again. If that doesn't work, create a new project and paste your code into it. Your operator+ compiles and produces the correct output for the input you give it when I run the code.

Nope, not working, tried closing compiler, copy code into new program, still getting error message:
AOef4_8.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\OefeningenLeenAmmeraal\LAOef4_8\LAOef4_8.cpp(30) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.

LAOef4_8.obj - 1 error(s), 0 warning(s)

THe code I'm trying to compile is:

#include "stdafx.h"
#include <iostream>

using namespace std;

short gcd(short numerator, short denominator);

class Fraction
{
public:
	Fraction ( short a = 0, short b = 0) : numerator (a), denominator (b){}
	
	friend Fraction operator* (const Fraction& firval, const Fraction& secval);
	friend Fraction operator/ (const Fraction& firval, const Fraction& secval);

	friend Fraction operator+ (const Fraction& firval, const Fraction& secval);
	friend Fraction operator- (const Fraction& firval, const Fraction& secval);

	Fraction reduce();

	void print()
	{
		cout<< numerator << "/" << denominator <<endl;
	}

private:
	short numerator, denominator;
};

Fraction operator* (const Fraction& firval, const Fraction& secval)
{
		Fraction result;
		
		result.numerator = firval.numerator * secval.numerator;
		result.denominator = firval.denominator * secval.denominator;

		result.reduce();

		return result;
}

Fraction operator/ (const Fraction& firval, const Fraction& secval)
{
	Fraction result;

	result.numerator = firval.numerator * secval.denominator;
	result.denominator = firval.denominator * secval.numerator;

	result.reduce();

	return result;
}

Fraction operator+ (const Fraction& firval, const Fraction& secval)
{
	Fraction result;
	
	short mult = gcd(firval.denominator, secval.denominator);

	result.denominator = (firval.denominator * secval.denominator) / mult;
	result.numerator = (firval.numerator * secval.denominator) + (secval.numerator * firval.denominator);

	result.reduce();

	return result;
}

Fraction operator- (const Fraction& firval, const Fraction& secval)
{
	Fraction result;

	short mult = gcd(firval.denominator, secval.denominator);

	result.denominator = (firval.denominator * secval.denominator) / mult;
	result.numerator = (firval.numerator * secval.denominator) - (secval.numerator * firval.denominator);

	result.reduce();

	return result;
}

Fraction Fraction::reduce()
{
	short div = gcd(numerator, denominator);

	numerator /= div;
	denominator /= div;

	return *this;
}

int main()
{
	Fraction fract1 (4, 5), fract2 (3, 8), c;

	cout<<"Fraction 1 and 2 are:"<<endl;
	fract1.print();
	fract2.print();cout<<endl;

	cout<<"Multiplied:"<<endl;
	c = fract1 * fract2;

	c.print();cout<<endl;

	cout<<"Divided:"<<endl;
	c = fract1 / fract2;

	c.print();cout<<endl;

	cout<<"Added:"<<endl;
	c = fract1 + fract2;

	c.print();cout<<endl;

	cout<<"Subtracting:"<<endl;
	c = fract1 - fract2;

	c.print();cout<<endl;

	cin.get();

	return 0;
}

short gcd(short numerator, short denominator)
{
	short temp;

	do
	{
		temp = (denominator% numerator);
		denominator = numerator;
		numerator = temp;
		if (temp == 0)
			return 1;
	}
	while (denominator% numerator != 0);

	return temp;
}

I'm giving up, atleast for tonight, will continue tomorrow morning tough ;)

The only thing I can think of is that stdafx.h isn't mixing well with your code. It couldn't hurt to start working with empty projects to avoid those silly precompiled headers. There's nothing wrong with your code, it compiles and runs on multiple implementations.

Well, I'm glad the code is working atlast :cheesy: Thanks for the help Narue.

Wouldn't mind trying out to make a project without those pre compiled headers :D

Only thing is, how do I do this :?:

Ive allready tried to but without any succes, could you tell me the steps I had to take to run my program succesfully?

Choose new --> Win32 Console Application --> Project Name --> An empty project. Sofar so good, but then.

I get on the left side the project name(workspace) and the right side is empty, how do I connect those two?

Click New Text File?

Copy my code into that?

When I do that, I can copy the header files aswell, but they do not show up in the workspace?

So, big question is, how do I link them together, meaning workspace and the program I copy?

>Sofar so good, but then.
Then you right click on the Source folder and choose Add New. From there it's only a matter of creating a .cpp file.

Hi Narue,

What I did:

File --> New --> Win32 Console Application --> Project Name --> An empty project --> Add files to folder --> choose --> C++ Source Files (.c;.cpp;.cxx;.tli) --> write --> filename (name project + .cpp) --> click on New Text File or double click on name project.cpp in the workspace to open to create new file --> copy code into new file --> Ctrl F7 --> result, the same ERROR code once again :sad:

Ive tried the same with another exercise in wich operators + and - weren't used and it worked like a charm :rolleyes:

I give up Narue, I'll change the + and - operator towards the Fraction operator+ (const Fraction& secval) type. This way, Ive got both ways shown in one exercise :)

Whatever, as long as it works. But the first chance you get, trash VC++ 6 and get a better compiler.

Wich do you consider being a good (affordable) compiler?

void print()
{
	(denominator < 0 ? cout<<"- " << numerator <<"/" << - denominator : 
	cout<< numerator <<"/" << denominator <<endl);
}

Ive been changing this piece of code so that it takes account of negative fractions, this way I get - 1/2 for example instead of 1/ -2.
Would you have done this in a separate function or like I did in void print()?

Is there a way that I could alter this code so that I could write
_ (1_2 <-- underneath eachother) in a relative simple way? Or do I have to make serious adjustments to achieve that?

cout<<"Fractions 1 and 2 are:"<<endl;
cout<<"Multiplied:"<<endl;
cout<<"Divided:"<<endl;
cout<<"Added:"<<endl;
cout<<"Subtracting:"<<endl;

Is there a way to decrease the amount 'cout' in main but still have the possibility to write a destinctive name?

>Wich do you consider being a good (affordable) compiler?
If you want an IDE then Dev-C++ is both free and good. The latest VC++ is only $99US for the standard edition, and Borland Builder is also $99US. Other options are Borland 5.5, which is free, or Comeau, probably the most conforming C++ compiler available (available for $50US).

>Would you have done this in a separate function or like I did in void print()?
It depends on your needs. What you have is a valid solution.

>Is there a way that I could alter this code so that I could write
>_ (1_2 <-- underneath eachother) in a relative simple way?
Unless something like this is acceptable then no:

#include <iostream>

using namespace std;

int main()
{
  cout<< 1 <<"\n-\n"<< 2 <<endl;
}

>Is there a way to decrease the amount 'cout' in main but still have the possibility to write a destinctive name?
Can you rephrase that?

>It depends on your needs. What you have is a valid solution
Then I'll leave it as it is.

>Unless something like this is acceptable then no:
Then I'll leave it as it is.

>Can you rephrase that?
Is there a way that when I write the operator I'm using in main it automatically writes a piece of code that explains what I'm going to do?

Like for instance when I type: c = fract1 * fract2; when executed, I would get automatically: Multiplied: - 1/2.

This way, I don't have to write cout's everytime I use a version of the operators.
Could this be done by writing the couts in the memberfunctions themselves?

I was thinking of grouping the text into one member function and depending on wich operator is used in main, call that particular piece of text?

Does that make sence :-|

>Could this be done by writing the couts in the memberfunctions themselves?
Yes, though it's generally a better idea to avoid output in value producing member functions. A fraction class can be useful, and you may not want operator* to write to cout if you use the class in future projects.

Though a clean solution is to think polymorphically from the start:

#include <iostream>

using namespace std;

class original_test {
public:
  original_test ( int init ): data ( init ) {}
public:
  virtual void print() const { cout<< data; }
private:
  int data;
};

class new_test: public original_test {
public:
  new_test ( int init ): original_test ( init ) {}
public:
  virtual void print() const { cout<<"Print: "; original_test::print(); }
};

int main()
{
  original_test t1 ( 10 );
  new_test t2 ( 20 );

  t1.print(); cout<<endl;
  t2.print(); cout<<endl;
}

Polymorphism, hmmm...polymorphism oh... yep found it --> Chapter 7 Section 4 :cheesy:

Guess It'll have to wait abit before I get there, but, great to hear there is a way to avoid so many cout's :D

Well, I'm starting Chapter 5 now, Templates, pointers and datastructures :!:

Thanks for the help Narue, going to read now about 40 pages, after that, it's exercise time again ;)

Class solved.

>Well, I'm starting Chapter 5 now, Templates, pointers and datastructures
Templates work too :):

#include <iostream>

using namespace std;

struct noverbose {
  static void get() {}
};

struct verbose {
  static void get() { cout<<"Get: "; }
};

template <typename policy = noverbose>
class test {
public:
  test ( int init ): data ( init ) {}
public:
  virtual void print() const { policy::get(); cout<< data; }
private:
  int data;
};

int main()
{
  test<> t1 ( 10 );
  test<verbose> t2 ( 20 );

  t1.print(); cout<<endl;
  t2.print(); cout<<endl;
}
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.