### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`` ``````#include #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
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include "Frac.h" #include 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
 ``123456789101112131415161718192021222324252627282930`` ``````#ifndef DEF_FRAC #define DEF_FRAC #include 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.