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++.
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
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;
}
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
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;
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
In WinAPI almost all structures and classes are in all caps so that might just be a convention you follow.
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
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.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608