Wrong output

when i ask the user to enter two inputs of type "int", and he enters a "letter = which is of type char" the compiler calls the default constructor that initializes an object to 0 (that is, to 0/1). can someone plz check the lines 89-99!!

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
#include <iostream>
#include <fstream>
using namespace std;

class Rational
{
	friend bool operator==(const Rational& p, const Rational& q);
	friend Rational operator+(const Rational& p, const Rational& q);
	friend Rational operator*(const Rational& p, const Rational& q);
	friend Rational operator-(const Rational& p, const Rational& q);
	friend bool operator<(const Rational& p, const Rational& q);
	friend bool operator>(const Rational& p, const Rational& q);
	friend ostream& operator<<(ostream& os, const Rational& p);
	friend istream& operator>>(istream& is, Rational& p);
private:
	int x;
	int y;
public:
	Rational();
	Rational(int xx, int yy);
	Rational(int xxx);
};
Rational::Rational()
{
	x=0;
	y=1;
}
Rational::Rational(int xx, int yy)
{	
	if(yy==0)
	{
		exit(1);
		yy=1;
	}
	else	
	{	x=xx;
		y=yy;
	}
}
Rational::Rational(int xxx)
{
	int h=1;
	y=h;
	x=xxx;
}

bool operator==(const Rational& p, const Rational& q)
{
	if((p.x/p.y)==(q.x/q.y))
	return true;
	else
	return false;
}
Rational operator+(const Rational& p, const Rational& q)
{
	Rational z(((p.x*q.y)+(p.y*q.x)), (p.y*q.y));
	return z;
}
Rational operator*(const Rational& p, const Rational& q)
{
	Rational z(p.x*q.x, p.y*q.y);
	return z;
}
	Rational operator-(const Rational& p, const Rational& q)
	{
		Rational z(((p.x*q.y)-(p.y*q.x)), (p.y*q.y));
		return z;
	}
	bool operator<(const Rational& p, const Rational& q)
	{
		if((p.x/p.y)<(q.x/q.y))
			return true;
		else 
			return false;
	}
	bool operator>(const Rational& p, const Rational& q)
	{
		if((p.x/p.y)>(q.x/q.y))
			return true;
		else 
			return false;

	}
	ostream& operator<<(ostream& os, const Rational& p)
	{
		os<<p.x<<"/"<<p.y<<endl;
		return os;
	}
	istream& operator>>(istream& is, Rational& p)
	{
		bool to_check;
		is>>p.x>>p.y;
		if(p.y==0)
		{
			to_check = false;
			exit(1);
		}
		return is;
	}
int main()
{
	Rational x; //this will call default constructor to initialize x to 0/1
	Rational y(3); //this should initialize y to 3/1
	Rational z(2, 3); //this will be a rational number 2/3
	Rational k, v;
	cout<<x; //it prints rational number in the form numerator/denominator
	cout<<y;
	cout<<z;
	if(x<y) cout<<"x is smaller than y\n";
	else if(x==y) cout<<"x is equal to y\n";
	else if(x>y) cout<<"x is greater than y\n";
	Rational a = y+z;
	cout<<"y+z="<<a;
	Rational b = y-z;
	cout<<"y-z="<<b;
	Rational c = y*z;
	cout<<"y*z="<<c;
	cout<<"Enter a rational number:";
	cin>>x;
	cout<<"You entered "<<x;
	ifstream ifs;
	ifs.open("inputfile.txt");
	ifs>>k>>v;
	ifs.close();
	ofstream ofs;
	ofs.open("output.txt");
	ofs<<"K= "<<k<<"V= "<<v<<"K+V= "<<k+v<<"K-V= "<<k-v<<"K*V= "<<k*v;
	system("pause");
	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
Rational::Rational(int xx, int yy)
{	
	if(yy==0)
	{
		exit(1);  // here is the problem
		yy=1;    // and here
	}
	else	
	{	x=xx;
		y=yy;
	}
}
if yy=0 , then you exit from 'Constructor', this make no sense. Just cancel the row exit(1);
Another problem:
1
2
3
4
5
6
7
bool operator<(const Rational& p, const Rational& q)
	{
		if((p.x/p.y)<(q.x/q.y)) //<----- just here
			return true;
		else 
			return false;
	}

The result of a division of two 'int' value is a 'int' value, and consist of the inegral part of the division, for example '3/2' is '1.5' but the 'int' result takes only the integral part '1' . You can see, the 'int' result of '3/2' and '4/3' is the same, although the two razional are different. there are two solution for this, cast the values to (double) before dividing, or use the comparition (num1*den2 < num2*den1). this is for the 'operator>' and 'operator==' too.
Finaaaaly, i got a reply. thx m8
EGAD Eyad! It sounds like you're complaining about a 90 minute response time for free forum help.
That sounds pretty damn good to me!
Topic archived. No new replies allowed.