i'm trying to add two fractions via a function and it works.
the problem is that i have to print the fraction in the main( and i have absolutly no idea how to do that)
other than that the program works fine.

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int frac(int,int,int,int);
void main()
{
clrscr();
int a,b,c,d;
cout<<"Enter fraction numbers"<<endl;
cin>> a>> b>> c>> d;
frac(a,b,c,d);

getch();
}
int frac(int a, int b, int c, int d)
{
int e,f;
e= (a*d + c*b);
f=d*b;
cout<<e<<'/'<<f;
return(e,f);
}

help me out please!!

Recommended Answers

All 5 Replies

You cannot return multiple values at once unless they are bound within a structure or an array (even then you are just returning the place in member where they start). There are two ways that you can do this (there are more):
1) Pass values by reference and modify one set as your answer
2) Create a structure and return the value in it

Below is code I put together based off yours but using a structure for the fractions.

#include <stdio.h>
#include <conio.h>

typedef struct
{
	int numerator;
	int denominator;
}FRACTION;

FRACTION frac(FRACTION, FRACTION);

int main()
{
	FRACTION first, second, result;
	printf("Enter fraction numbers ");
	scanf("%d%d%d%d",&first.numerator,&first.denominator,&second.numerator,&second.denominator);
	result = frac(first, second);

	printf("%d/%d", result.numerator, result.denominator);

	getch();
	return 0;
}

FRACTION frac(FRACTION a, FRACTION b)
{
	a.numerator = (a.numerator*b.denominator + b.numerator*a.denominator);
	a.denominator = a.denominator*b.denominator;
	return a;
}

EDIT: I thought this was for C since you are using C headers. Rewriting this in C++.

In C++ I'd use classes and their operators to add the fractions together.

You can play around with overloading the >> and << operators to make calling input/output neater.

#include <iostream>
#include <conio.h>
using namespace std;

class FRACTION
{
	int numerator;
	int denominator;

	public:

	FRACTION()
	{
		cin >> numerator >> denominator;
	}
	FRACTION(int num, int denom)
	{
		numerator = num;
		denominator = denom;
	}

	int GetNumerator()
	{
		return numerator;
	}

	int GetDenominator()
	{
		return denominator;
	}

	friend void operator+=(FRACTION &lhs, FRACTION rhs)
	{
		lhs.numerator = lhs.numerator*rhs.denominator + rhs.numerator*lhs.denominator;
		lhs.denominator *= rhs.denominator;
	}

	friend FRACTION operator+(FRACTION lhs, FRACTION rhs)
	{
		return FRACTION(lhs.numerator*rhs.denominator + rhs.numerator*lhs.denominator, rhs.denominator*lhs.denominator);
	}

};

int main()
{

	cout << "Enter fraction numbers ";
	//using += operator
	FRACTION first, second;
	first += second;

	cout << first.GetNumerator() << "/" <<  first.GetDenominator() << endl;

	cout << "Enter fraction numbers ";
	//using + operator
	FRACTION third, fourth, result(0,0);
	result = third + fourth;

	cout << result.GetNumerator() << "/" <<  result.GetDenominator() << endl;

	getch();
	return 0;
}

In C++ I'd use classes and their operators to add the fractions together.

You can play around with overloading the >> and << operators to make calling input/output neater.

#include <iostream>
#include <conio.h>
using namespace std;

class FRACTION
{
	int numerator;
	int denominator;

	public:

	FRACTION()
	{
		cin >> numerator >> denominator;
	}
	FRACTION(int num, int denom)
	{
		numerator = num;
		denominator = denom;
	}

	int GetNumerator()
	{
		return numerator;
	}

	int GetDenominator()
	{
		return denominator;
	}

	friend void operator+=(FRACTION &lhs, FRACTION rhs)
	{
		lhs.numerator = lhs.numerator*rhs.denominator + rhs.numerator*lhs.denominator;
		lhs.denominator *= rhs.denominator;
	}

	friend FRACTION operator+(FRACTION lhs, FRACTION rhs)
	{
		return FRACTION(lhs.numerator*rhs.denominator + rhs.numerator*lhs.denominator, rhs.denominator*lhs.denominator);
	}

};

int main()
{

	cout << "Enter fraction numbers ";
	//using += operator
	FRACTION first, second;
	first += second;

	cout << first.GetNumerator() << "/" <<  first.GetDenominator() << endl;

	cout << "Enter fraction numbers ";
	//using + operator
	FRACTION third, fourth, result(0,0);
	result = third + fourth;

	cout << result.GetNumerator() << "/" <<  result.GetDenominator() << endl;

	getch();
	return 0;
}

Usually ALL_CAPS word are used for constants. So not to confuse future programmers and to promote following convention, please stop promoting this. And also there is no need for getch(), a good IDE will pause your console before exiting, or use cin.

@OP: I'm assuming you don't know about classes and structs yet, so in that case you can pass parameters by reference like so.

#include<cstdio>
#include<iostream>

void addFraction(int n1, int d1, int n2, int d2, int& numerator, int& denominator){
  numerator = n1 * d2 + n2 * d1; //save numerator 
  denominator = d1 * d2; ///save denominator
}
int main(){
 int n1 = 0, d1 = 0;
 int n2 = 0, d2 = 0;
 cin >> n1 >> d1; //get first fraction
 cin >> n2 >> d2; //get second fraction
 int resultNumerator = 0, resultDenominator = 0;
 addFraction(n1,d1,n2,d2,resultNumerator,resultDenominator); //save result in resultNumerator and resultDenominator
 cout << n1 << "/" << d1 << " + " << n2 << "/" << d2 << " = " << resultNumerator << "/" << resultDenominator << endl;
}

In WinAPI almost all structures and classes are in all caps so that might just be a convention you follow.

In WinAPI almost all structures and classes are in all caps so that might just be a convention you follow.

True in a sense that it could be a convention one can follow, but it doesn't mean its a good convention. But you have to understand that the windows API is really old. Even the windows API uses all caps for constant data types, but they also use all caps for certain structures. And they also mix up the two, that is use all caps for constant structure. If you were developing the windows api library, then you should follow their convention, but since this is C++, you should follow C++ convention. And although there aren't a standard convention, there are some widely used convention and its best not only for the creator but also for future maintainer to use convention that they will naturally understand.

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.