Fraction Calculator Program

Nov 23, 2010 at 2:37am
I have a program thats supposed to add, subtract, multiply, or divide fractions depending on what the user chooses. I have functions for scanning the two fractions, adding, subtracting, multiplying, dividing, and a calculating function. Yes I need all these functions because the teacher wants them. I don't get a compiler error or a runtime error but I get a weird negative number over itself.

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
/*
	lab7_2.cpp
	

	Program asks the user if they would like to add, subtract, multiply, or               divide two fractions
	User enters two fractions.
	Program calculates the new fraction.

	Inputs: n1, d1, n2, d2
	Outputs: n3, d3
	Process:

*/

# include <iostream>
using namespace std;

bool scanFraction(int &num, int &den);
int add(int n1, int d1, int n2, int d2, int &n3, int &d3);
int subtract(int n1, int d1, int n2, int d2, int &n3, int &d3);
int multiply(int n1, int d1, int n2, int d2, int &n3, int &d3);
int divide(int n1, int d1, int n2, int d2, int &n3, int &d3);
int calculate(int choice, int n1, int d1, int n2, int d2, int &n3, int &d3);

int main(){
	int n1, d1, n2, d2, choice;
	int n3;
	int d3;
	
	cout << "1: Add\n";
	cout << "2: Subtract\n";
	cout << "3: Multiply\n";
	cout << "4: Divide\n";
	cout << "0: Exit\n";
	cin >> choice;
	scanFraction(n1, d1);
	scanFraction(n2, d2);
	
	calculate(choice, n1, d1, n2, d2, n3, d3);

	cout << "Your new fraction is " << n3 << "/" << d3 << endl;
	return 0;
}

int getChoice(int &choice){

	return choice;
}

bool scanFraction(int &num, int &den){
	char slash;
	cout << "Enter a fraction: ";
	cin >> num >> slash >> den;
	return slash == '/';
}

int add(int n1, int d1, int n2, int d2, int &n3, int &d3){
	n3 = ((n1*d2) + (n2*d1)) / (d1* d2);
	d3 = d1 * d2;

return n3 / d3;
}

int subtract(int n1, int d1, int n2, int d2, int &n3, int &d3){
	n3 = (n1*d2) - (n2*d1);
    d3= d1 * d2;

return n3 / d3;
}

int multiply(int n1, int d1, int n2, int d2, int &n3, int &d3){
	n3 = n1 * n2;
	d3= d1 * d2;

return n3 / d3;
}

int divide(int n1, int d1, int n2, int d2, int &n3, int &d3){
	n3 = n1 * d2;
	d3 = n2 * d1;

return n3 / d3;
}

int calculate(int choice, int n1, int d1, int n2, int d2, int &n3, int &d3){

	switch(choice){
		case '0':
			exit(-1);
			break;
		case '1':
			add(n1, d1, n2, d2, n3, d3);
			break;
		case '2':
			subtract(n1, d1, n2, d2, n3, d3);
			break;
		case '3':
			multiply(n1, d1, n2, d2, n3, d3);
			break;
		case '4':
			divide(n1, d1, n2, d2, n3, d3);
			break;
		default:
			break;
		return choice;
	}
}





my sample output is

1: Add
2: Subtract
3: Multiply
4: Divide
0: Exit
1
Enter a fraction: 3/4
Enter a fraction: 4/5
Your new fraction is -858993460/-858993460
Press any key to continue . . .


Any ideas why?
Yes I am a beginner so don't hate too much.
Nov 23, 2010 at 4:47am
You are reading choice as an integer but your switch statement is comparing it
against the ASCII value of '0', '1', etc. rather than the integer value, hence you
are always hitting the default case.

Nov 23, 2010 at 4:49pm
Thank you.
Topic archived. No new replies allowed.