D
DoRJo
Guest
I have a code I am working on, but have been working on it for too long. The functions work independently of each other, but together they do not. As well, could someone give me an laymens explanation on functions. I have read, and I have read but they still confuse me. Hence why my code is not working. I have tried declaring int a, b, and c in a few spots but it nothing works. If someone could look at it and point me in the direction on where I need to focus my attention, I would really appreciate it.
#include "pch.h"
#include <iostream>
#include <conio.h>
using namespace std;
int gcd(int a, int b)
{
if (a= 0)
return b;
return gcd(b % a, a);
}
class Fraction
{
int numerator, denominator, whole_number;
int newFraction;
public:
int a;
int b;
int c;
const static char Slash;
char aSlash();
int enterFractionValue();
void setValues(int a, int b, int c);
int reduceFraction(int a, int b, int c);
void displayFraction();
};
const char Fraction::Slash = '/';
char Fraction::aSlash()
{
return Slash;
}
int Fraction::enterFractionValue()
{
while ((whole_number != 0) && (numerator != 0))
{
//Read whole_number
cout << "Enter a whole number: >> ";
cin >> whole_number;
//Read numerator
cout << "Enter a numerator: >> ";
cin >> numerator;
//Read denominator
cout << "Enter a denominator: >> ";
cin >> denominator;
while (denominator == 0)
{
cout << "Zero is not a valid dominator value" << endl;
cout << "Enter a different denominator value: >> ";
cin >> denominator;
}
return whole_number, numerator, denominator;
}
};
void Fraction::setValues(int a, int b, int c)
{
this->whole_number = a;
this->numerator = b;
this->denominator = c;
}
int Fraction::reduceFraction(int a, int b, int c)
{
int gcf;
int y;
if (b >= c)
{
a += b / c;
b = b % c;
}
gcf = 1;
for (y = b; y > 0; --y)
{
if (b % y == 0 && c == 0)
{
gcf = y;
y = 0;
}
}
b /= gcf;
c /= gcf;
return (a, b, c);
//test output
cout << a << " " << b << "/" << c << endl;
}
void Fraction::displayFraction()
{
cout << a << " " << b << Slash << c << endl;
}
int main()
{
Fraction newFraction;
int a;
int b;
int c;
newFraction.enterFractionValue();
newFraction.setValues(a, b, c);
newFraction.reduceFraction(a, b, c);
newFraction.displayFraction();
return 0;
}
Continue reading...
#include "pch.h"
#include <iostream>
#include <conio.h>
using namespace std;
int gcd(int a, int b)
{
if (a= 0)
return b;
return gcd(b % a, a);
}
class Fraction
{
int numerator, denominator, whole_number;
int newFraction;
public:
int a;
int b;
int c;
const static char Slash;
char aSlash();
int enterFractionValue();
void setValues(int a, int b, int c);
int reduceFraction(int a, int b, int c);
void displayFraction();
};
const char Fraction::Slash = '/';
char Fraction::aSlash()
{
return Slash;
}
int Fraction::enterFractionValue()
{
while ((whole_number != 0) && (numerator != 0))
{
//Read whole_number
cout << "Enter a whole number: >> ";
cin >> whole_number;
//Read numerator
cout << "Enter a numerator: >> ";
cin >> numerator;
//Read denominator
cout << "Enter a denominator: >> ";
cin >> denominator;
while (denominator == 0)
{
cout << "Zero is not a valid dominator value" << endl;
cout << "Enter a different denominator value: >> ";
cin >> denominator;
}
return whole_number, numerator, denominator;
}
};
void Fraction::setValues(int a, int b, int c)
{
this->whole_number = a;
this->numerator = b;
this->denominator = c;
}
int Fraction::reduceFraction(int a, int b, int c)
{
int gcf;
int y;
if (b >= c)
{
a += b / c;
b = b % c;
}
gcf = 1;
for (y = b; y > 0; --y)
{
if (b % y == 0 && c == 0)
{
gcf = y;
y = 0;
}
}
b /= gcf;
c /= gcf;
return (a, b, c);
//test output
cout << a << " " << b << "/" << c << endl;
}
void Fraction::displayFraction()
{
cout << a << " " << b << Slash << c << endl;
}
int main()
{
Fraction newFraction;
int a;
int b;
int c;
newFraction.enterFractionValue();
newFraction.setValues(a, b, c);
newFraction.reduceFraction(a, b, c);
newFraction.displayFraction();
return 0;
}
Continue reading...