calling a function in void?

Hello World!

Very new to the intriguing world of C++. Loving the struggle so far however ran into a road block. My assignment is to create a complex class where fractions are entered, converted to decimal and reduced. My trouble is twofold: 1- after compiling, the "reduce" function returns only the value for 1 in the numerator and "nan" after the denominator. I am thinking my syntax is wrong somewhere. 2- is there a way to create the reduce function in void and calling the gcd function into it (within complex)? Any pointers would be appreciated! I'm learning what looks to be an older C++ version so I might be a bit behind on the latest ASCII rules.

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

//class stuff
class fraction // a/bi
{
    private:
        int n,d,temp,x,y,g; // the variables which "make up" the fraction number a/bi        
        
        
    public:
        //constructor declarations
        fraction(); //default constructor
        fraction(double); //1-parameter constructor
        fraction(double,double); //maximal constructor (because user sets both a and b)
        
        //class function declarations
        
        double num(); //gives the value of numerator
        double denom(); //gives the value of denominator
        double convert(); // converts to decimal - (num*1.00)/denom
        double gcd(); // finds gcd
        double reduce(); // reduces the fraction
        void reciprocal();
        void show(); //displays a/bi to monitor
        void set(double,double); //changes a/bi to c/di
};

//Constructor definitions
fraction::fraction() // 0/1
{
    n=0,d=1;
}

fraction::fraction(double x) // x/1
{
    n=x,d=1;
}

fraction::fraction(double x, double y) // x/yi
{
    n=x, d=y;
}

//Function definitions
double fraction::num() //a
{
    return n;
}

double fraction::denom() //b
{
    return d;
}

double fraction::convert() //fraction to decimal
{
    return ((n*1.00)/d);
}

double fraction::gcd()
{
    while(d!=0)
{
        temp=n;
        n=d;
        d=temp%d;
        
}
        return n;
}

double fraction::reduce()
{
       cout << n/(gcd()) << "/" << d/(gcd());
}
    


void fraction::show() //displays a/bi to monitor
{
    cout << n << "/" << d;
};

void fraction::reciprocal()
{
    cout << d << "/" << n;
};    

void fraction::set(double e, double f) //changes a/bi to c/di
{
    n=e, d=f;
};

fraction operator* (fraction z1, fraction z2)
{
    double n=z1.num()*z2.num();
    double d=z1.denom()*z2.denom();
    
    fraction z3(n,d);
    return z3;
}    


int main()
{
    fraction x; //uses default constructor (no parameters provided)
    fraction y(5); //uses the 1 - parameter contructor (1 - parameter provided)
    fraction z(2,-3); //uses the maximal constructor (2 - parameters provided)
    
    // What can I do with x, y, and z? real, imag, modulus, conjugate, show, set

    cout << "The numerator of x is " << x.num() 
         << " and the denominator of x is " << x.denom() << endl;
    cout << "The numerator of y is " << y.num() 
         << " and the denominator of y is " << y.denom() << endl;
    cout << "The numerator of z is " << z.num() 
         << " and the denominator of z is " << z.denom() << endl << endl;
         
    cout << "Where x=";
    x.set(3,4); //uses set function x
    x.show();
    cout << ", the decimal value of x is " << x.convert() << endl << endl;

    fraction d;
    cout << "Enter a fraction: numerator 'Enter' denominator\n";
    int a,b;
    cin >> a >> b;
    z.set(a,b);
    cout << a << "/" << b << " converted to a decimal is= " << z.convert() << endl;
    z.set(a,b);
    cout << a << "/" << b << " has a GCD = " << z.gcd() << "\n\n";
    z.set(a,b);
    cout << z.reduce() << "\n\n";

    
    system("PAUSE");
    return 0;
}Put the code you need help with here.
First, your numerator and denominator are defined as ints. So, the constructors and all the member funcs all have to take in ints and return a int (except for the convert to decimal function). Second, you didn't return anything for reduce(), so it should return void.

The problems with reduce are all in the function gcd() (although I don't have the least bit of idea how you got the gcd with the code in the function).

The numerator is always one, because, notice that in gcd() you changed the value of the numerator directly. So after calling gcd(), the returned value and the numerator now are the same. That's what gets you the 1 all the time.

I am not sure why the denom is NaN (Not a Number, in case you are wondering). It should be "inf" (infinity) or 0 or something like that. One thing is certain, though. In gcd the only way to get out of the loop is for the denom to be 0. So you are outputting the result of 0 divided by whatever value n is.
Only if n is 0 can the output be "NaN". But if n is 0, the first one would also be "NaN", not 1.

Fact: In C++,
x(positive, non-zero) / 0.0 = inf(infinity)
x(negative, non-zero) / 0.0 = -inf
0.0 / 0.0 = NaN
0.0 / x(anything, including infinity) = 0.0



Sorry I'm not sure what you are talking about in 2.
It doesn't make sense for gcd() to be a member of fraction. Single rational numbers don't have GCDs. A GCD is a property of a pair of integers. Therefore, gcd() should be a global function:
 
int gcd(int a, int b);
Thanks sooo much! I will think your replies through and come right back!
Topic archived. No new replies allowed.