adding rational numbers using classes

I'm trying to write a program that uses rational numbers (entered as a whole numerator and a whole denominator). Eventually I want it to add/subtract/multiply/divide rational numbers but for now I'm trying to get it to add two rational numbers. Ignore the green part of the code. Could someone please explain to me why my add function doesn't work?

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
#include <iostream>

using namespace std;

class rational
{
public:
	void output(ostream& out);
	void set(short num, short den);
	void set(short integer);

	short get_numerator();
	short get_denominator();
	rational add(rational number);
	rational add(short number);
	rational sub(rational number);
	rational sub(short number);
	rational mul(rational number);
	rational mul(short number);
	rational div(rational number);
	rational div(short number);
	rational neg();

private:
	short numerator;
	short denominator;
};

int main()
{
	char loop = 'y';
	do
	{
		rational rational1, rational2;
		cout << "Input the numerator and denominator: ";
		short num, den;
		cin >> num >> den;
		rational1.set(num, den);
		cout << "Input the numerator and denominator: ";
		cin >> num >> den;
		rational2.set(num, den);

		cout << "What would you like to do?\n"
			 << "1) add\n"
			 << "2) subtract\n"
			 << "3) multiply\n\n";
		char choice;
		cin >> choice;
		while(choice != '1' && choice != '2' && choice != '3')
		{
			cout << "Invalid choice. What would you like to do?\n"
				 << "1) add\n"
				 << "2) subtract\n"
				 << "3) multiply\n\n";
			cin >> choice;
		}

		rational result;

		
		if(choice == '1')
		{
			result = rational1.add(rational2);
			cout << "The result is: ";
			result.output(cout);
		}/*
		else if(choice == '2')
		{
			result = rational1.sub(rational2);
			cout << "The result is: ";
			result.output(cout);
		}
		else if(choice == '3')
		{
			result = rational1.mul(rational2);
			cout << "The result is: ";
			result.output(cout);
		}*/

		cout << "Would you like to run the program again? (y/n)? ";
		cin >> loop;
		loop = tolower(loop);
		while(loop != 'y' && loop != 'n')
		{
			cout << "Incorrect output. Would you like to run the program again? ";
			cin >> loop;
		}
	}while(loop == 'y');

	cout << "Press a key to exit: ";
	char exit;
	cin >> exit;

	return 0;
}

void rational::output(ostream& out = cout)
{
	out << numerator << "/" << denominator;
}

void rational::set( short num, short den)
{
	numerator = num;
	denominator = den;
}

void rational::set( short integer)
{
	numerator = integer;
	denominator = 1;
}

short rational::get_denominator()
{
	return denominator;
}

short rational::get_numerator()
{
	return numerator;
}


rational rational::add(rational number)
{
	numerator = numerator*number.denominator + denominator*number.numerator;
	denominator = denominator*number.denominator;
}

rational rational::add(short integer)
{
	numerator = numerator + integer*denominator;
	rational result;
	result.set(numerator, denominator);
	return result;
}
Could someone please explain to me why my add function doesn't work? (followed by 137 lines of code)


From Welcome -- read before posting
Don't ask others to debug your broken code without giving a hint what sort of problem they should be searching for. Posting a few hundred lines of code, saying "it doesn't work", will get you ignored. Posting a dozen lines of code, saying "after line 7 I was expecting to see <x>, but <y> occurred instead" is much more likely to get you a response.


I copied your code and built it. My IDE (xcode) gave one warning: "Control reaches end of non-void function" on line 129.
You want to return a new rational number, not change the one called upon.
1
2
3
4
5
6
7
8
//Line 125
rational rational::add(rational number) //We need to return a "rational"
{
	rational sum;
	sum.numerator = (numerator * number.denominator) + (denominator * number.numerator);
	sum.denominator = denominator * number.denominator;
	return sum;
}

Try that... Also, where are your constructors? If you allow your constructor to take an int (or short) you could avoid the overloaded functions. Also, consider passing your rational numbers around as references in the method's parameters. And why use the name "add" when you could use "operator+"...?
Thank you for your responses.

Blackavar -- I will be more careful in the future.

Mathhead -- Thank you, you made me realize my mistake and helped me realize where I can improve my code and for that I'm grateful.
Topic archived. No new replies allowed.