Problems with using class and switch case to compile a fraction calculator

hey guys, I got a little problem when running the following code, it is about fraction computation. I dont know why it can't output the result I needed, could anybody help?

#include <iostream>
#include <string>
using namespace std;

class Fraction
{
private:
int top; //Numerator
int bottom; //Denominator
Fraction F1();
Fraction F2();
public:
Fraction() //default constructor
{
top = 0;
bottom = 1;
}
Fraction(int T, int B) //constructor with both numerator and denominator
{
top = T;
if (B==0)
{
cout<<"Invalid number"<<endl;
exit(0);
}
else
bottom = B;
}

Fraction add(Fraction F1, Fraction F2) //peform addition
{
int T = F1.top*F2.bottom+F1.bottom*F2.top;
int B = F1.bottom*F2.bottom;
return Fraction( T, B);
}
Fraction subtract(Fraction F1, Fraction F2) //perform subtraction
{
int T = F1.top*F2.bottom-F1.bottom*F2.top;
int B = F1.bottom*F2.bottom;
return Fraction( T, B);
}
Fraction multiple(Fraction F1, Fraction F2) //perform multiplication
{
int T = F1.top*F2.top;
int B = F1.bottom*F2.bottom;
return Fraction( T, B);
}
Fraction divide(Fraction F1, Fraction F2) //perform division
{
int T = F1.top*F2.bottom;
int B = F1.bottom*F2.top;
return Fraction( T, B);
}
void compare(Fraction F1, Fraction F2) //perform comparasion
{
if (F1.top*F2.bottom<F2.top*F1.bottom)
cout<<"The first fraction is smaller"<<endl;
if (F1.top*F2.bottom==F2.top*F1.bottom)
cout<<"The two fractions are equal"<<endl;
if (F1.top*F2.bottom>F2.top*F1.bottom)
cout<<"The first fraction is greater"<<endl;
}
void input() //Input function
{
cout<<"Please enter the numerator:"<<endl;
cin>>top;
cout<<"Please enter the denominator:"<<endl;
cin>>bottom;
}
void output() //Output function
{
if (bottom == 1)
cout<< top << endl;
else
cout << top << "/" << bottom <<endl;
}
};


int main()
{
Fraction result;
Fraction a;
Fraction b;
int choice;
cout<<"Please enter two fractions"<<endl;
a.input();
b.input();
cout<<"*******************************"<<endl;
cout<<"* Chose one number to continue*"<<endl;
cout<<"* 1.Add *"<<endl;
cout<<"* 2.Subtract *"<<endl;
cout<<"* 3.Multiply *"<<endl;
cout<<"* 4.Division *"<<endl;
cout<<"* 5.Compare *"<<endl;
cout<<"* other keys to exit *"<<endl;
cout<<"*******************************"<<endl;
cin>>choice;
switch(choice)
{
case '1':
result.add(a,b);
result.output();
break;
case '2':
result.subtract(a,b);
result.output();
break;
case '3':
result.multiple(a,b);
result.output();
break;
case '4':
result.divide(a,b);
result.output();
break;
case '5':
result.compare(a,b);
break;
default:
break;
}
return 0;
}



Please use code tags when you post.
http://www.cplusplus.com/articles/z13hAqkS/

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

class Fraction
{
private:
	int top; //Numerator
	int bottom; //Denominator
public:
	Fraction() //default constructor
	{ 
		top = 0;
		bottom = 1;
	}
	Fraction(int T, int B) //constructor with both numerator and denominator
	{
		top = T;
		if (B==0)
		{
			cout<<"Invalid number"<<endl;
			exit(0);
		}
		else
			bottom = B;
	}

	void add(Fraction F1, Fraction F2) //peform addition
	{
		top = F1.top*F2.bottom+F1.bottom*F2.top;
		bottom = F1.bottom*F2.bottom;
		
	}
	void subtract(Fraction F1, Fraction F2) //perform subtraction
	{
		top = F1.top*F2.bottom-F1.bottom*F2.top;
		bottom = F1.bottom*F2.bottom;
	}
	void multiple(Fraction F1, Fraction F2) //perform multiplication
	{
		top = F1.top*F2.top;
		bottom = F1.bottom*F2.bottom;
		
	}
	void divide(Fraction F1, Fraction F2) //perform division
	{
		top = F1.top*F2.bottom;
		bottom = F1.bottom*F2.top;
	}
	void compare(Fraction F1, Fraction F2) //perform comparasion
	{
		if (F1.top*F2.bottom<F2.top*F1.bottom)
			cout<<"The first fraction is smaller"<<endl;
		if (F1.top*F2.bottom==F2.top*F1.bottom)
			cout<<"The two fractions are equal"<<endl;
		if (F1.top*F2.bottom>F2.top*F1.bottom)
			cout<<"The first fraction is greater"<<endl;
	}
	void input() //Input function
	{
		cout<<"Please enter the numerator:"<<endl;
		cin>>top;
		cout<<"Please enter the denominator:"<<endl;
		cin>>bottom;
	}
	void output() //Output function
	{
		if (bottom == 1)
			cout<< top << endl;
		else
			cout << top << "/" << bottom <<endl;
	}
};


int main()
{
	Fraction result;
	Fraction a;
	Fraction b;
	int choice;
	cout<<"Please enter two fractions"<<endl;
	a.input();
	b.input();
	cout<<"*******************************"<<endl;
	cout<<"* Chose one number to continue*"<<endl;
	cout<<"* 1.Add *"<<endl;
	cout<<"* 2.Subtract *"<<endl;
	cout<<"* 3.Multiply *"<<endl;
	cout<<"* 4.Division *"<<endl;
	cout<<"* 5.Compare *"<<endl;
	cout<<"* other keys to exit *"<<endl;
	cout<<"*******************************"<<endl;
	cin>>choice;
	switch(choice)
	{
	case 1:
		result.add(a,b);
		result.output();
		break;
	case 2:
		result.subtract(a,b);
		result.output();
		break;
	case 3:
		result.multiple(a,b);
		result.output();
		break;
	case 4:
		result.divide(a,b);
		result.output();
		break;
	case 5:
		result.compare(a,b);
		break;
	default:
		break;
	}
	
	return 0;
}


There are better ways to do this, but I don't feel like rewriting it.
Last edited on
Topic archived. No new replies allowed.