Fraction Class HELP!

closed account (ivDwAqkS)
I don't have any syntax error, however it pops up a windows of "Fraction.cpp Has stopped" why? can you tell me whats wrong? and when I declare the second fraction variable "Fraction frac2;" without arguments it run, but doesn't sum. why? D: please help me :c

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

class Fraction{
public:
    void input();  
    void output();    //output
    Fraction() {set(0, 1);}    //default constructor
    Fraction(int num, int den) {set(num, den);}    //constructor (int, int)
    void set(int num, int den)    //setter
        {n = num; d = den; normalize();}
    int get_num() {return n;}
    int get_den() {return d;}
    //operations
    Fraction add(Fraction other);
    Fraction sub(Fraction other);
    Fraction mul(Fraction other);
    Fraction div(Fraction other);
private:
    int gcd(int n, int d);
    int lcm(int n, int d);
    int n, d;
    void normalize();
};

int main(){

    Fraction frac1(3,2), frac2(4,6), frac3;
    int choice;
    string symbol, operator1;

    cout << "Calculator Options: \n";
    cout << "\tAddition.\n";
    cout << "\tSubtraction.\n";
    cout << "\tMultiplication.\n";
    cout << "\tDivision.\n";
    cout << "Enter your choice: ";
    cin >> choice;

    cout << "Fraction 1 is: ";
    frac1.output();
    cout << endl;
    cout << "Fraction 2 is: ";
    frac2.output();
    cout << endl;

    switch(choice){
        case 1:
            symbol = " + ";
            operator1 = "sum";
            frac3 = frac1.add(frac2); break;
        case 2:
            symbol = " - ";
            operator1 = "rest";
            frac3 = frac1.sub(frac2); break;
        case 3:
            symbol = " x ";
            operator1 = "multiplication";
            frac3 = frac1.mul(frac2); break;
        case 4:
            symbol = " / ";
            operator1 = "division";
            frac3 = frac1.div(frac2); break;
        default:
            symbol = " + ";
            operator1 = "sum";
            frac3 = frac1.add(frac2);
    }

    cout << "The ";
    cout << operator1;
    cout << " of: ";
    frac1.output();
    cout << symbol;
    frac2.output();
    cout << " = ";
    frac3.output();
    cout << endl << endl;

    return 0;
}

void Fraction::normalize(){
    if (d == 0 || n == 0){
        n = 0;
        d = 1;
    }
    if (d < 0){
        n *= -1;
        d *= -1;
    }

    int g = gcd(n, d);
    n /= g;
    d /= g;
}

int Fraction::gcd(int n, int d){
    if(d == 0)
        return abs(n);
    else
        return gcd(d, n&d);//ERROR FOUND CHANGE & TO %
}

int Fraction::lcm(int n, int d){
    int g = gcd(n, d);
    return n/g *d;
}

void Fraction::output(){
    if(d == 1)
        cout << n;
    else
        cout << n << "/" << d;

}

void Fraction::input(){
    cout << "Enter numerator: ";
    cin >> n;
    cout << "Enter denominator: ";
    cin >> d;
}


//sum
Fraction Fraction::add(Fraction other){
    int l = lcm(d, other.d);
    int n1 = l/d;
    int n2 = l/other.d;
    int num1 = (n * n1) + (other.n * n2);
    int den1 = l;

    Fraction frac33;
    frac33.set(num1, den1);
    return frac33;
}
//subtraction
Fraction Fraction::sub(Fraction other){
    int l = lcm(d, other.d);
    int num1 = n * other.d - other.n * d;
    int den1 = l;

    Fraction frac3(num1, den1);
    return frac3;
}
//multiplication
Fraction Fraction::mul(Fraction other){
    int num1 = n * other.n;
    int den1 = d * other.d;

    Fraction frac3(num1, den1);
    return frac3;
}
//division
Fraction Fraction::div(Fraction other){
    int num1 = n * other.d;
    int den1 = d * other.n;

    Fraction frac3(num1, den1);
    return frac3;
}
Last edited on
1
2
3
4
5
6
int Fraction::gcd(int n, int d){
    if(d == 0)
        return abs(n);
    else
        return gcd(d, n&d);
}


This is causing a stack overflow (it never stops). Whatever you are trying to do I doubt n&d will give you what you want.
closed account (ivDwAqkS)
it is supposed to give me the gcf of the numerator and denominator.
Btw thank you, I found I mistyped & instead of %
Last edited on
Topic archived. No new replies allowed.