Rational Number using class

I complete this program, but the only thing is that it won't execute the program. Any idea what I did wrong????

edit- i found the problem, the other problem is the program is not running as it is suppose to.

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cctype>

using namespace std;

class Rational
{
	private:
		int numerator;
		int denominator;		
		int GCD(int, int); //Determine the GCD

	public:
		Rational();
		Rational(int, int);
		Rational(int);

		int getNumerator();
		int getDenominator();

		Rational add(Rational);
		Rational sub(Rational);
		Rational mul(Rational);
		Rational div(Rational);

		void reduction();
		void Numerator(int);
		void Denominator(int);
		void printans(); 
		void printnum();

};
bool valid(int den )// check if valid
{
  return (den > 0 || den < 0);
}
int main()
{
	cout << fixed << showpoint << setprecision(2);  //2 significant figures
	char again = 'y'; //Loop
	
	cout << "This program perfroms several arithmetic operations on two rational numbers." << endl << endl;
	
	while (toupper(again) == 'Y') //run again
	{
		int num, den;
		cout << "Enter a rational number (a/b): ";
		string f1;
		getline( cin, f1 );// get the entire string from the input
		cout << endl;

		 istringstream ss( f1 );
			char slash; //slash
			ss >> num >> slash >> den;

		return ss.eof() && (slash == '/') && valid(den);//Check string
		while (!(den) == 0) 
		{
			cout << "Error: Re-Enter the rational number (a/b): ";
			fflush(stdin);
			cin.clear();
		}
		Rational x = Rational(num, den);
		cout << "Just checking, make sure the number is simplified." << endl << endl;
		x.reduction(); //reduce user input
		
		cout << "Enter a rational number (a/b): ";
		string f2;
		getline( cin, f2 );// get the entire string from the input
		cout << endl;

		istringstream ss1( f2 );
		char divisor; //slash
		ss1 >> num >> divisor >> den;

		return ss.eof() && (divisor == '/') && valid(den);//Check string

		while (!(den) == 0) 
		{
			cout << "Error: Re-Enter the rational number (a/b): ";
			fflush(stdin);
			cin.clear();
		}
		Rational z = Rational(num, den);
		cout << "Just checking, make sure the number is simpfied." << endl << endl;
		x.reduction(); //reduce user input

		Rational rat;

		cout << endl << "Results of arithmetic operations:" << endl << endl;
		x.printans();
		cout << " + ";
		z.printans();
		cout << " = ";
		rat = x.add(z);
		rat.printans();
		cout << " = ";
		rat.printnum();
		cout << endl;

		x.printans();
		cout << " - ";
		z.printans();
		cout << " = ";
		rat = x.sub(z);
		rat.printans();
		cout << " = ";
		rat.printnum();
		cout << endl;

		x.printans();
		cout << " * ";
		z.printans();
		cout << " = ";
		rat = x.mul(z);
		rat.printans();
		cout << " = ";
		rat.printnum();
		cout << endl;

		x.printans();
		cout << " / ";
		z.printans();
		cout << " = ";
		
		if(z.getDenominator() != 0) 
		{
			rat = x.div(z);
			rat.printans();
			cout << " = ";
			rat.printnum();
		}
		else
		{
			cout << "ERROR! DIVIDE BY ZERO  =  UNDEFINED!";
		}
		
		cout << endl << endl;

		cout << endl << endl; 
		cout << "Run Again (y/n)? ";//Ask to run again
		cin >> again;
		cout << endl; 
		fflush(stdin);
		cin.clear();
		
	}

	fflush(stdin);
	cin.clear();
	cout << endl << endl;
	cout << "Programmer: Adasuna" << endl << endl;//Display name
	cout << "Good bye! Press <Enter> to end the program...";//Have user exit 
	cin.get();
	return 0;

}

Rational::Rational()
{
	//Set number to one if nothing specified
	numerator = 1;
	denominator = 1;
}

Rational::Rational(int n, int d)
{
	numerator = n;
	denominator = d;
}

Rational::Rational(int n)
{
	//Set denominator to 1 if only one value passed in
	numerator = n;
	denominator = 1;
}

int Rational::GCD(int a, int b) 
{	
	if (b == 0)
	{
		return a;
	}		
	else
	{
		return GCD(b, a % b);
	}
}

void Rational::reduction()
{
	int temp = GCD(numerator, denominator);
	numerator = numerator / temp;
	denominator = denominator / temp;
}

void Rational::Denominator(int d)
{
	denominator = d;
}

void Rational::Numerator(int n)
{
	numerator = n;
}

int Rational::getDenominator() 
{
	return denominator;
}

int Rational::getNumerator() 
{
	return numerator;
}

void Rational::printans() 
{
	if(denominator == 1) 
	{
		cout << numerator;
	}
	else if(numerator == 0) 
	{
		cout << numerator;
	}
	else //if in form a/b
	{
		cout << numerator << "/" << denominator;
	}	
}

void Rational::printnum() 
{
	double temp = numerator;
	cout << temp / denominator;
}

Rational Rational::add(Rational temp) 
{
	int Denom = denominator * temp.getDenominator(); //get new denominator
	int Numon = (numerator * temp.getDenominator()) + (denominator * temp.getNumerator()); //calculate new numerator
	Rational back = Rational(Numon, Denom);
	back.reduction(); //reduce answer
	return back;
}

Rational Rational::sub(Rational temp) 
{
	int Denom = denominator * temp.getDenominator(); //get new denominator
	int Numon = (numerator * temp.getDenominator()) - (denominator * temp.getNumerator()); //calculate new numerator
	Rational back = Rational(Numon, Denom);
	back.reduction(); //reduce answer
	return back;
}

Rational Rational::mul(Rational temp) 
{
	int Denom = denominator * temp.getDenominator(); //get new denominator
	int Numon = numerator * temp.getNumerator(); //calculate new numerator
	Rational back = Rational(Numon, Denom);
	back.reduction(); //reduce answer
	return back;
}

Rational Rational::div(Rational temp) 
{
	int temp1 = temp.getDenominator();
	temp.Denominator(temp.getNumerator());
	temp.Numerator(temp1);
	int Denom = denominator * temp.getDenominator(); //get new denominator
	int Numon = numerator * temp.getNumerator(); //calculate new numerator
	Rational back = Rational(Numon, Denom);
	back.reduction(); //reduce answer
	return back;
}
Last edited on
Can anyone help me?
Topic archived. No new replies allowed.