Class function performing on an operator

I figured out how the operator errors but now I am unsure about how to make fracMult(f2) be preformed on f1. I would really appreciate any input about the right code to make it work

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
  class fraction

{
    private:
        int numerator;
        int denom;
        bool positive;
    
    public:
        void inputFrac();
        void printFrac();
     
        fraction fracMult(fraction b);
        fraction fracDiv(fraction b);
        fraction fracAdd(fraction b);
        fraction fracSub(fraction b);
};

 void fraction::printFrac()
{
    if (!positive)
    {
    cout << "-";
    }
    cout << numerator << " / " << denom;
}
void fraction::inputFrac()
{    
    cout<<"Please input the numerator ";
    cin>>numerator;
    cout<< "Please input the denominator ";
    cin>>denom;           
}

 fraction fraction::fracMult(fraction b)
{
     
     numerator=b.numerator;
     denom=b.denom;
}

int main(int argc, char** argv) {

    fraction f1, f2, fresult;
    
    f1.inputFrac(); //input the first fraction
    f2.inputFrac(); //input the second fraction
    cout<<endl;
    f1.printFrac();
    cout<<endl;
    f2.printFrac();
    cout<<endl;

    cout << "The result of a * b is: ";
    
    fresult = f1.fracMult(f2); // calculate a * b
    fresult.printFrac(); // print out the result 
Last edited on
fracMult has return type void. It doesn't return anything. That's why it doesn't work when you try to assign the result to the variable fresult.
fracMult is void (no return type) so line 57 is wrong. fresult is not needed, along with top & bottom inside of fracMult.

Additionally, how do you set fraction::positive? Right now I see that you don't.
Last edited on
There tallyman i moved it over here and changed the code
f1.fracmult(f2) actually does what you want it to do. It multiplies f1 & f2, and assigns the resulting value to f1. So, you should make the return type void once again, and remove fresult altogether if that is what you're going for.
To add to what has been said. Your method fracMult is doing everything apart from multiplying the two fractions. Check line 38 and 39!
You are simply just assigning the numerator and denominator of f2 to f1.

In as much as I do not intend to query the purpose of your program, what is this code
1
2
3
4
if (!positive)
    {
    cout << "-";
    }

used to archive in printFrac because I don't see where else, that variable is been toggled.
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/179750/
Topic archived. No new replies allowed.