Division par 0 en C++ avec C ++ buider

Résolu
Bonjour,

Alors voila mon problème, j'ai "créé" ma propre calculatrice et j'aimerai que la division par zéro soit interdite. Mais je n'ai rien appris qui pourrait me le permettre quelqu'un pourrait t'il m'aider? Merci d'avance et bonen fin de journée.

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>

#include "ufmMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfmMain *fmMain;

char cMonNombre[20];
float iNombre1=0;
float iNombre2=0;
float fResultat=0;
bool bAddition = false;
bool bSoustraction = false;
bool bMultiplication = false;
bool bDivision = false;

void __fastcall TfmMain::bu8Click(TObject *Sender)
{
StrCat(cMonNombre,bu8->Caption.c_str());
edAffichage->Text= cMonNombre;
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::bu9Click(TObject *Sender)
{
StrCat(cMonNombre,bu9->Caption.c_str());
edAffichage->Text= cMonNombre;
}
//---------------------------------------------------------------------------

void __fastcall TfmMain::buPlusClick(TObject *Sender)
{
bAddition=true;
sprintf(cMonNombre,edAffichage->Text.c_str());
iNombre1 = StrToInt(cMonNombre);
edAffichage->Text="";
sprintf(cMonNombre,edAffichage->Text.c_str());
}
//---------------------------------------------------------------------------

void __fastcall TfmMain::buEgalClick(TObject *Sender)
{
sprintf(cMonNombre,edAffichage->Text.c_str());
iNombre2 = StrToInt(cMonNombre);
sprintf(cMonNombre,buDeco0->Caption.c_str());

if (bAddition==true)
{
fResultat=iNombre1 + iNombre2;
edAffichage->Text = fResultat;
}

if (bSoustraction==true)
{
fResultat=iNombre1 - iNombre2;
edAffichage->Text = fResultat;
}

if (bMultiplication==true)
{
fResultat=iNombre1 * iNombre2;
edAffichage->Text = fResultat;
}

if (bDivision==true)
{
fResultat=iNombre1 / iNombre2;
edAffichage->Text = fResultat;
}

bAddition = false;
bSoustraction = false;
bMultiplication = false;
bDivision = false;

}
//---------------------------------------------------------------------------

void __fastcall TfmMain::buMoinsClick(TObject *Sender)
{
bSoustraction=true;
sprintf(cMonNombre,edAffichage->Text.c_str());
iNombre1 = StrToInt(cMonNombre);
edAffichage->Text="";
sprintf(cMonNombre,edAffichage->Text.c_str());
}
//---------------------------------------------------------------------------

void __fastcall TfmMain::buFoisClick(TObject *Sender)
{
bMultiplication=true;
sprintf(cMonNombre,edAffichage->Text.c_str());
iNombre1 = StrToInt(cMonNombre);
edAffichage->Text="";
sprintf(cMonNombre,edAffichage->Text.c_str());
}
//---------------------------------------------------------------------------

void __fastcall TfmMain::buDiviserClick(TObject *Sender)
{
bDivision=true;
sprintf(cMonNombre,edAffichage->Text.c_str());
iNombre1 = StrToInt(cMonNombre);
edAffichage->Text="";
sprintf(cMonNombre,edAffichage->Text.c_str());
}
//---------------------------------------------------------------------------

Pour les premiers nombres je ne les ais pas mis mais ils sont pareils aux derniers.

Merci d'avoir pris du temps pour me lire.
Configuration: Windows XP
Internet Explorer 7.0

1 réponse

  1. void __fastcall TfmMain::buEgalClick(TObject *Sender)
    { .....
    ....
    if (bDivision==true)
    {
    if(iNomre2 != 0)
    {fResultat=iNombre1 / iNombre2;
    edAffichage->Text = fResultat;
    }
    else
    edAffichage->Text = "Erreur Division par 0";
    }
    1. void __fastcall TfmMain::buEgalClick(TObject *Sender)
      { .....
      ....
      if (bDivision==true)
      {
      if(iNombre2 != 0)
      {fResultat=iNombre1 / iNombre2;
      edAffichage->Text = fResultat;
      }
      else
      edAffichage->Text = "Erreur Division par 0";
      }

      merci pour ton aide