operator+ problem

Hello everyone,
It's been a lot of time since I started to try to find the problem in my code, and now, I'm wondering what's wrong with it, really...
As you will see, it's still a WIP, so I just care about the adding part.
Sorry for the non english code :S

main
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
#include <iostream>
#include "Frac.h"

using namespace std;

// cout <<

ostream& operator<<( ostream &flux, Frac const& frac)
{
    frac.afficher(flux);
    return flux;
}

//Liste des fonctions

Frac add(Frac frac1, Frac frac2 ,Frac frac3)
{
    frac3 = frac1 + frac2;
    return frac3;
}

Frac sub(Frac frac1, Frac frac2, Frac frac3)
{
    frac3 = frac1 - frac2;
    return frac3;
}

Frac mult(Frac frac1, Frac frac2, Frac frac3)
{
    frac3 = frac1 * frac2;
    return frac3;
}

Frac div(Frac frac1, Frac frac2, Frac frac3)
{
    frac3 = frac1 / frac2;
    return frac3;
}

void valeurexacte(Frac eg)
{
    int rep(0);
    cout <<"Souhaitez-vous avoir la valeur exacte de cette fraction?";
    cin >> rep;
    switch (rep)
    {
        case 1:
        cout << endl;
        cout << "La voici: " << eg.getm_valeurexacte() << endl;
        break;

        case 2:
        //Oh no you deedn't!
        break;
    }
}


int main()
{
    int choixOperation(1);
    int denom(1);
    int num(1);
    Frac fracresult;
    cout << endl;

    do
    {
    cout << "0 - Quitter" << endl;
    cout << "1 - Additionner" << endl;
    cout << "2 - Soustraire" << endl;
    cout << "3 - Multiplier" << endl;
    cout << "4 - Diviser" << endl;

    cin >> choixOperation;
    switch (choixOperation){
    case 0:
        return 0;
        break;

    case 1:{
        cout <<"Entrez la valeur du numerateur de la premiere fraction a additionner: ";
        cin >> num;
        cout << endl;
        cout <<"Le denominateur maintenant: ";
        cin >> denom;
        cout << endl;
        Frac frac1(num, denom);
        Frac frac2(3, 4);
        add(frac1, frac2, fracresult);
        cout << frac1 <<" + "<< frac2 <<" = "<< fracresult << endl;
        valeurexacte(fracresult);
        break;}

    case 2:{
        Frac frac1;
        Frac frac2(5, 4);
        sub(frac1, frac2, fracresult);
        cout << fracresult;
        valeurexacte(fracresult);
        break;}

    case 3:{
        Frac frac1;
        Frac frac2(5, 4);
        mult(frac1, frac2, fracresult);
        cout << frac1;
        valeurexacte(fracresult);
        break;}

    case 4:{
        Frac frac1;
        Frac frac2(5, 4);
        div(frac1, frac2, fracresult);
        cout << fracresult;
        valeurexacte(fracresult);
        break;}

    default:
        cout << "Tapez un nombre entre 0 et 4." << endl;
        break;
    }
    }
    while (choixOperation !=0);
    return 0;
}


.cpp
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
#include "Frac.h"
#include <cstdlib>
using namespace std;

Frac::Frac(int numerateur, int denominateur) : m_numerateur(numerateur), m_denominateur(denominateur), m_valeurExacte(m_numerateur / m_denominateur)
{

}

void Frac::afficher(ostream &flux) const
{
    flux << m_numerateur << "/" << m_denominateur;
}

//Les opérateurs simples

Frac operator+(Frac const& a, Frac const& b)
{
    Frac copie(a);
    return copie+=b;
}

Frac operator-(Frac const& autre)
{
    Frac copie(autre);
    copie *= -1;
    return autre;
}

Frac operator-(Frac const& a, Frac const& b)
{
    Frac resultat(a);
    return resultat-=b;    //Idem
}

Frac operator*(Frac const& a, Frac const& b)
{
   Frac copie(a);
   return copie*=b;
}

Frac operator/(Frac const& a, Frac const& b)
{
    Frac resultat(a);
    return resultat/=b;    //Idem
}


//Operateurs plus complexes

Frac& Frac::operator+=(Frac const& autre)
{
    //On effectue les calculs comme à l'école
    m_numerateur = autre.m_denominateur * m_numerateur + m_denominateur * autre.m_numerateur;
    m_denominateur *= autre.m_denominateur;

    return *this;
}

Frac& Frac::operator-=(Frac const& autre)
{
    m_numerateur = autre.m_denominateur * m_numerateur - m_denominateur * autre.m_numerateur;
    m_denominateur *= autre.m_denominateur;

    return *this;
}

Frac& Frac::operator*=(Frac const& autre)
{
    m_numerateur *= autre.m_numerateur;
    m_denominateur *= autre.m_denominateur;

    return *this;
}

Frac& Frac::operator/=(Frac const& autre)
{
    m_numerateur *= autre.m_denominateur;
    m_denominateur *= autre.m_numerateur;

    return *this;
}

//Les autres methodes:

double Frac::getm_valeurexacte()
{
    return m_valeurExacte;
}


.h
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
#ifndef DEF_FRAC
#define DEF_FRAC

#include <iostream>

class Frac
{
    public:
    Frac(int numerateur = 0, int denominateur = 1);
    //Frac(int entier = 1);
    double getm_valeurexacte();
    void afficher(std::ostream &flux) const;

    Frac& operator+=(Frac const& autre);
    Frac& operator-=(Frac const& autre);
    Frac& operator*=(Frac const& autre);
    Frac& operator/=(Frac const& autre);

    private:
    int m_numerateur;
    int m_denominateur;
    double m_valeurExacte;
};

    Frac operator+(Frac const& a, Frac const& b);
    Frac operator-(Frac const& a, Frac const& b);
    Frac operator*(Frac const& a, Frac const& b);
    Frac operator/(Frac const& a, Frac const& b);

#endif 


What I get is something like this: 2/3 + 3/4 = 0/1

I know this is a long code but you know only a little part of it is important...
Thank you everyone ;)
You are passing values to add() functions by value (by copy), so actual fracresult isn't changed. Your IDE should give a warning "ignored return value".
quick dirty fix: fracresult = add(frac1, frac2, fracresult);
Last edited on
Ok thank you very much!
I did this mistake already but I corrected it, but now it's the second time I do it.
Thank you very much for your help =)
Topic archived. No new replies allowed.