Part of our assignment is to reduce two user inputted fractions so they are in their simplest form. The error messages I get mostly have to do with my "num" and "denom" for numerator and denomenator being undeclared identifiers, but there may be more. I'm kinda bad at C++. I'd really appreciate any help I can get.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
class Fraction
{
private:
int num;
int denom;
char something;
public:
Fraction() {}
void reduce();
friend istream operator>> (istream, Fraction);
friend ostream& operator<< (ostream&, const Fraction&);
};
Fraction::Fraction (int numerator, int denominator, char something)
{
num = numerator;
denom = denominator;
something = a;
}
void reduce()
{
int n, m, r, sign;
n = num;
m = denom;
if (num = 0)
{
denom = 1;
return;
}
else
{
sign = 1;
if (num < 0)
{
sign = -1;
num = -num;
}
if (denom < 0)
{
sign = -sign;
denom = -denom;
}
r = n % m;
while (r != 0){
n = m;
m = r;
r = n % m;
}
num = num * sign / m;
denom = denom * sign / m;
}
}
istream operator>> (istream input, int num, int denom, char something, Fraction& oneRatio)
{
input >> num >> something >> denom;
return input;
oneRatio.reduce();
}
ostream& operator<< (ostream& output, const Fraction& oneRatio)
{
output << oneRatio.num;
if (oneRatio.denom != 1)
output << "/" << oneRatio.denom;
return output;
}
int main()
{
Fraction fract_1, fract_2;
cin >> fract_1 >> fract_2;
cout << "The entered fraction, reduced is "<< fract_1 << endl
<< "The entered fraction, reduced is " << fract_2 << endl << endl;
return 0;
}
All of my errors concern the use of num and denom in the function reduce. When I set n=num and m=denom I get "undeclared identifier" and everytime I use num and denom for the rest of the function, I get "identifier not found, even with argument-dependent lookup"
I also get the error 'Fraction::Fraction(int,int,char)' : overloaded member function not found in 'Fraction' in the constructor