input isn't passing into a class.
Sep 30, 2019 at 5:59am UTC
results of add(), sub(), etc. aren't printing out...
Here's the actual code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
//input validation
int inputInteger(string prompt)
{
int input;
do
{
cout << prompt;
if (!(cin >> input))
{
cout << "ERROR-1A: Invalid input. Must be an integer type.\n" ;
cin.clear();
cin.ignore(999, '\n' );
}
else
break ;
} while (true );
return input;
}
class Rational
{
private :
int numerator;
int denominator;
void normalize();
public :
Rational();
Rational(int numerator, int denominator);
Rational(int numerator);
Rational add(Rational r)
{
Rational fraction;
fraction.denominator = this ->denominator * r.denominator;
fraction.numerator = this ->numerator * r.denominator +
this ->denominator * r.numerator;
return fraction;
}
Rational sub(Rational r)
{
Rational fraction;
fraction.denominator = this ->denominator * r.denominator;
fraction.numerator = this ->numerator * r.denominator -
this ->denominator * r.numerator;
return fraction;
}
Rational mul(Rational r)
{
Rational fraction;
fraction.denominator = this ->denominator * r.denominator;
fraction.numerator = this ->numerator * r.numerator;
return fraction;
}
Rational div(Rational r)
{
Rational fraction;
fraction.denominator = this ->denominator * r.numerator;
fraction.numerator = this ->numerator * r.denominator;
return fraction;
}
int GCD(int numerator, int denominator)
{
if (denominator == 0)
return numerator;
else if (denominator > numerator)
return GCD(denominator, numerator);
else
return GCD(denominator, numerator % denominator);
}
void input(istream& in);
void output(ostream& out);
};
Rational::Rational() : numerator(0), denominator(0) {};
Rational::Rational(int numerator, int denominator)
{
this ->numerator = numerator;
this ->denominator = denominator;
}
Rational::Rational(int numerator)
{
this ->numerator = numerator;
}
void Rational::normalize()
{
int gcd = GCD(numerator, denominator);
cout << (numerator / gcd) << "/" << (denominator / gcd);
}
void Rational::input(istream& in)
{
this ->numerator = inputInteger("Enter numerator: " );
this ->denominator = inputInteger("Enter denominator: " );
Rational fraction(numerator,denominator);
}
void Rational::output(ostream& out)
{
Rational fraction;
fraction.input(cin);
out << "Normalized: " ;
fraction.normalize();
out << endl;
}
int main()
{
cout << "Rational Number 1: " << endl;
Rational fraction0;
fraction0.output(cout);
cout << endl;
cout << "Rational Number 2: " << endl;
Rational fraction00;
fraction00.output(cout);
cout << endl;
cout << "Addition: " ; fraction0.add(fraction00);
cout << endl;
cout << "Subtraction: " ; fraction0.sub(fraction00);
cout << endl;
cout << "Multiplication: " ; fraction0.mul(fraction00);
cout << endl;
cout << "Division: " ; fraction0.div(fraction00);
cout << endl;
}
SAMPLE OUTPUT
Rational Number 1:
Enter numerator: 4
Enter denominator: 26
Normalized: 2/13
Rational Number 2:
Enter numerator: 23
Enter denominator: 69
Normalized: 1/3
Addition:
Subtraction:
Multiplication:
Division:
Last edited on Sep 30, 2019 at 5:26pm UTC
Sep 30, 2019 at 7:02am UTC
If you mean it isn't printing the result of add(), sub() etc. then that is perfectly true ... because you haven't written anything that would output them!
Sep 30, 2019 at 5:25pm UTC
Yes, how would one go about this? Is it in the main that I need to fix or the class?
Sep 30, 2019 at 5:56pm UTC
Just use your normalize() member function to output them in main()... though it might have been more natural to do it with an output() member function ... which you are currently using for input!
Sep 30, 2019 at 7:22pm UTC
oh, we're only supposed to use normalize() to simplify the fraction...
i'm rethinking of restructuring because I think the input function is all incorrect too...? im supposed to input two fractions and then add/sub/mul/div them... i dont think my code is doing that...?
i tried doing this in my input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void Rational::input(istream& in)
{
this ->numerator = inputInteger("Enter numerator1: " );
this ->denominator = inputInteger("Enter denominator1: " );
Rational fraction1(numerator,denominator);
cout << "Normalized: " ;
fraction1.normalize();
cout << endl;
this ->numerator = inputInteger("Enter numerator1: " );
this ->denominator = inputInteger("Enter denominator1: " );
Rational fraction2(numerator, denominator);
cout << "Normalized: " ;
fraction2.normalize();
cout << endl;
cout << "Addition: " ; fraction1.add(fraction2);
}
still no output for addition...
Sep 30, 2019 at 7:29pm UTC
Why are you trying to do OUTPUT in your INPUT routine? Also, since it is a member function, why are you trying to input two other fractions in it?
And when you have done your adding ... why don't you output it?
Topic archived. No new replies allowed.