I'm not sure why this isn't working...

For some reason my decimals are coming out wrong. The program asks for your fractions, and will print out the fraction and the decimal underneath, except I can't get my decimals to come out right. I can easily get them to work if I just divide them in the print class, but the assignment calls for a decimal class... What am I doing wrong?

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


#include <iostream>
using namespace std;


class fraction{
private:
        int numerator, denominator;
public:
        float n, d, g, j;
        fraction();
        fraction(int n, int d=1);
        void print();
        void decimal();
        int set_numerator(int n);
        int set_denominator(int d);


};



fraction::fraction(){

        cout << " " << endl;
        cout << "Put in your fractions." << endl;
        cout << " " << endl;
        cout << "Numerator: ";
        cin >> n;
        cout << " " << endl;
        cout << "Denominator: ";
        cin >> d;
        cout << " " << endl;


        if (d == 0){

                cout << "CAN NOT DIVIDE BY ZERO" << endl;
                denominator == 1;
                }
        else {
                denominator = d;
                }
        }

int fraction::set_numerator(int n){

                numerator = n;
}

int fraction::set_denominator(int d){

                denominator = d;
}

void fraction::decimal(){

        if(d >= 2){
        g == (numerator/denominator);
        }
        else {
        j == numerator;
        }
}

void fraction::print(){

       if(d >= 2){
        cout << "Fraction: " << n << "/" << d << endl;
        cout << "Decimal:  " << g << endl;
        cout << " " << endl;
        } else {
        cout << "Fraction: " << n << "/" << "1" << endl;
        cout << "Decimal:  " << j << endl;
        cout << " " << endl;
        }
}

int main(){



fraction arrayOfFractions[3];
        arrayOfFractions[0].print();
        arrayOfFractions[1].print();
        arrayOfFractions[2].print();
        //arrayOfFractions[3].print();
        //arrayOfFractions[4].print();
        //arrayOfFractions[5].print();
        //arrayOfFractions[6].print();
        //arrayOfFractions[7].print();
        //arrayOfFractions[8].print();
        //arrayOfFractions[9].print();

}



What I'm getting:

Fraction: 1/2
Decimal: 0

Fraction: 3/4
Decimal: 5.88212e-39

Fraction: 5/6
Decimal: 0

Last edited on
Well, are you actually ever setting numerator to anything in the constructor?

If you don't, it will have a garbage value - the value its current memory area contains - which may be zero if it wasn't used before your program started.

Also, be aware that g == (numerator/denominator); is an integer division, compared to a floating point. You need to cast at least one variable to float or you'll lose the decimals.
g = static_cast<float> (numerator) / denominator;
ok, thank you for the help. I thought I was setting the values, but maybe I'm missing something? I'm not sure...
Topic archived. No new replies allowed.