Wrong execution?

Could someone help me out and point out what's wrong with this code?No errors are showing up in my compiler. It's supposed to take two rational numbers and add, subtract, multiply, divide them but when i run the program it comes out as only "0 + 0 = 0"

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  /* 
 * Nov 16 2016
 *
 * Assignment 8
 * exercise 9.15
 */

#include <iostream>
#include <iomanip>

using namespace std;



//Rational class definition
class Rational {
public:
	Rational ( int  = 0, int  = 1); //default case
	 Rational adding( const Rational &);  // a) adding two rational numbers
	 Rational subtracting(const Rational &); // b) subtracting two rational numbers
	 Rational multiplying(const Rational &);  // c) multiplying two rational numbers
	 Rational  dividing(const Rational &);    // d) dividing two rational numbers
	void printRational();   // e) printing in form of a/b, where a is numerator and b is denominator
	void printFloat();    // f) printing in floating point format

private:
	int numerator;
	int denominator;
	void reduce();
};


 Rational::Rational(int n, int d) {
	numerator = d<0 ? -n:n;   // to make sure that if a negative number is entered, it will be in the numerator
	denominator = d<0 ? -d:d;
	reduce();
}
Rational Rational::adding ( const Rational& a) {    //adding
	Rational r;

	r.numerator = a.numerator * denominator + a.denominator * numerator;
	r.denominator = a.denominator * denominator;
	if (denominator != 0)
	r.reduce();

	return r;
}

Rational Rational::subtracting (const Rational& s) {    //subtracting
	Rational r;

	r.numerator = s.denominator * numerator - denominator * s.numerator;
	r.denominator = s.denominator * denominator;
	if (denominator !=0)
	r.reduce();

	return r;
}

Rational Rational::multiplying (const Rational& m){     //multiplying
	Rational r;

	r.numerator = m.numerator * numerator;
	r.denominator = m.denominator * denominator;
	if (denominator != 0)
	r.reduce();

	return r;
}

 Rational Rational::dividing (const Rational& b){      //dividing
	Rational r;

	r.numerator = b.denominator * numerator;
	r.denominator = denominator * b.numerator;
	if (denominator != 0)
	r.reduce();

	return r;

}

void Rational::printRational(){     //printing the rational number
	if (denominator = 0)
		cout<< "Error" <<endl;
	else if (numerator == 0)
		cout<<0;
	else
		cout << numerator << "/" << denominator << endl;
	}


 void Rational::printFloat(){       //printing as a float

	if (denominator = 0)
		cout << "Error" <<endl;
	else
		cout << numerator / denominator << "." << numerator%denominator <<endl;

}

void Rational::reduce(){       //reduced rational number
	int n = numerator < 0 ? -numerator : numerator;
	int d = denominator;
	int largest = n > d ? n : d;

	int gcd = 0;

	for (int i = largest; i>= 2; i--)
		if(numerator % i == 0 && denominator % i == 0){
			gcd = i;
			break;
		}
	if (gcd != 0) {
		numerator /= gcd;
		denominator /= gcd;
		}
}





int main(){
	Rational x(-2/6), y(-14/-16), z, w(4);

//adding
	x.printRational();
	cout<< " + ";
	y.printRational();
	z = x.adding(y);
	cout<< " = ";
	z.printRational();
	cout << endl;
	z.printRational();
	cout<< " = ";
	z.printFloat();
	cout << endl;

// subtracting
	x.printRational();
		cout<< " - ";
		y.printRational();
		z = x.subtracting(y);
		cout<< " = ";
		z.printRational();
		cout << endl;
		z.printRational();
		cout<< " = ";
		z.printFloat();
		cout << endl;

// multiplying
		x.printRational();
			cout<< " * ";
			y.printRational();
			z = x.multiplying(y);
			cout<< " = ";
			z.printRational();
			cout << endl;
			z.printRational();
			cout<< " = ";
			z.printFloat();
			cout << endl;

//dividing
		x.printRational();
			cout<< " / ";
			y.printRational();
			z = x.adding(y);
			cout<< " = ";
			z.printRational();
			cout << endl;
			z.printRational();
			cout<< " = ";
			z.printFloat();
			cout << endl;

	return 0;
}





Last edited on
closed account (E0p9LyTq)
Your program is fine, your logic is running up against integer math.

Integers in C/C++ can not have a fractional component. They are truncated. 14/16 is less than one, the fractional part is chopped off when storing the result in an int variable.

http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c
Your constructor is expecting two separate integers.

Do you mean to call Rational x(-2,6) instead of Rational x(-2/6)?
Topic archived. No new replies allowed.