can someone help me figure out why my output is incorrect any help would be apprecaited

#include<iostream>
using namespace std;

class fractions
{
public:
	fractions fractions :: operator+(fractions f);
	fractions fractions :: operator-(fractions f);
	fractions fractions :: operator*(fractions f);
	fractions fractions :: operator/(fractions f);
	fractions addFraction(fractions);
	fractions subFraction(fractions);
	fractions multiFraction(fractions);
	fractions divideFraction(fractions);
	fractions();
	fractions(int, int);
	void getFraction();
	void printFraction();
private:
	int numerator;
	int denominator;
};

//method to add two fractions
fractions fractions :: addFraction(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator + denominator * f.numerator;
	return temp;
}
//method to add two fractions
fractions fractions :: operator+(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator + denominator * f.numerator;
	return temp;
}
//method to subtract two fractions
fractions fractions :: subFraction(fractions f)
{
    fractions temp;
    temp.denominator = denominator * f.denominator;
    temp.numerator = numerator * f.denominator - denominator * f.numerator;
    return temp;
}
//method to subtract two fractions
fractions fractions :: operator-(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator - denominator * f.numerator;
	return temp;
}
//method to multiply two fractions
fractions fractions :: multiFraction(fractions f)
{
    fractions temp;
    temp.denominator = denominator * f.denominator;
    temp.numerator = numerator * f.denominator * denominator * f.numerator;
    return temp;
}
//method to multiply two fractions
fractions fractions :: operator*(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator * denominator * f.numerator;
	return temp;
}
//method to divide to fractions
fractions fractions :: divideFraction(fractions f)
{
    fractions temp;
    temp.denominator = denominator * f.denominator;
    temp.numerator = numerator * f.denominator / denominator * f.numerator;
    return temp;
}
//method to divide two fractions
fractions fractions :: operator/(fractions f)
{
	fractions temp;
	temp.denominator = denominator * f.denominator;
	temp.numerator = numerator * f.denominator / denominator * f.numerator;
	return temp;
}


fractions::fractions()
{
	numerator = 0;
	denominator = 1;
}

fractions::fractions(int n, int d)
{
	numerator = n;
	denominator = d;
}

void fractions::printFraction()
{
	cout <<numerator<<'/'<<denominator<<endl;
}

void getFraction(int &numerator, int &denominator)
{
    cout<<"Please enter numerator";
	cin >>numerator;
	cout<<"Please enter denominator";
	cin >>denominator;
}

int main()
{
	int n,d;
	fractions f1(1,2);
	fractions f2();
	fractions f3;
	fractions f4;
    getFraction(n,d);
	fractions f5(n,d);
	

	f3 = f1.addFraction(f2);
	f4 = f3 + f1;
	f3.printFraction();
	f4.printFraction();
	
	f3 = f1.subFraction(f2);
	f4 = f3 - f1;
	f3.printFraction();
	f4.printFraction();
	
	f3 = f1.multiFraction(f2);
	f4 = f3 * f1;
	f3.printFraction();
	f4.printFraction();
	
	f3 = f1.divideFraction(f2);
	f4 = f3 / f1;
	f3.printFraction();
	f4.printFraction();
	system("pause");
	

}

Dani AI

Generated

Brief summary: the surprising numerators/denominators come from a handful of concrete mistakes rather than a mysterious runtime bug. Key issues are (1) an object declared as a function (the “most‑vexing‑parse”), (2) member declarations using a scope qualifier inside the class, (3) incorrect multiplication/division formulas and operator‑precedence problems, and (4) a mismatch between the declared member getFraction and the free function that’s defined. Fixing those will make the arithmetic behave as expected.

Common mistakes to fix (check each one):

  • A line that looks like a type name followed by empty parentheses declares a function, not an object. Declare the default object without parentheses so the default constructor actually runs.
  • Inside the class body do not repeat the class name with scope resolution for member declarations. Declare operators as members that take a const reference and are themselves const (to avoid copies and allow use on temporaries).
  • The multiply/divide implementations are wrong. Multiplication should multiply the two numerators and multiply the two denominators. Division should multiply the left numerator by the right denominator and the left denominator by the right numerator. Also add parentheses to force the intended order of operations.
  • getFraction is declared as a class method but the code defines a free function with a different signature. Either make it a proper member definition or remove the member declaration.
  • Always check for zero denominator and normalize the result (use gcd to reduce to lowest terms).

Practical debugging flow: replace the problematic declarations first (so default constructors run), correct one operator implementation at a time and recompile after each change, print numerator/denominator inside constructors and inside each operator to verify intermediate values, then add a small normalize() helper to reduce fractions. Prefer implementing the core arithmetic once (a private helper) and have operator+ call that helper to avoid duplicated logic.

As pointed out, the scope and copying issues matter; ’s request for expected output is valid for verification; and was right to flag the multiplication flaw. After the fixes above, results should match textbook fraction arithmetic and be far smaller and simpler than the large products seen in the original run.

Recommended Answers

All 4 Replies

It would help if you posted the output you were supposed to have and what output you got

First off, you cannot write fractions fractions :: operator/(fractions f); within the definition of a class.

Second, you have provided a default constructor that set the fraction to 0/1 . That gives perfectly acceptable results. So which test is wrong??

Third: You have not used references in many of your function , so there is lots of unnecessary copying

Forth: The biggest failing the the lack of code reuse. You have addFraction and operator+ using the smae code. PLEASE call one with the other. Otherwize you have two places to change each time and that leads to a mess.

It would help if you posted the output you were supposed to have and what output you got

the output im getting is

Please enter numerator2
Please enter denominator4
10/8
28/16
-2/8
-12/16
24/8
384/16
6/8
1/16
Press any key to continue . . .

its suppose to add subtract mulitplty etc fractions

Also see my post in your other thread about the flaw in your multiplication...

Stu, I'm thinking having 2 functions (one operator and one non-operator) might have been part of the assignment.

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.