Pas d'affichage pour un string

Résolu
Bonjour,
vraiment j'ai étonné pourquoi le programme n'affiche pas le résultat

#include<stdio.h>
#include<string.h>

void main()
 {
 int centaine, dizaine, unite, reste, y,i,chiffre;
 char lettre[256] ;
 strcpy(lettre, "");
 reste = chiffre;
 printf("donner le chiffre \n");
 scanf("%d",&chiffre);


 for(i=1000000000; i>=1; i/=1000)
 {
 y = reste/i;
 if(y!=0)
 {
 centaine = y/100;
 dizaine = (y - centaine*100)/10;
 unite = y-(centaine*100)-(dizaine*10);
 switch(centaine)
 {
 case 0:
 break;
 case 1:
 strcat(lettre,"cent ");
 break;
 case 2:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"deux cents ");
 else strcat(lettre,"deux cent ");
 break;
 case 3:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"trois cents ");
 else strcat(lettre,"trois cent ");
 break;
 case 4:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"quatre cents ");
 else strcat(lettre,"quatre cent ");
 break;
 case 5:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"cinq cents ");
 else strcat(lettre,"cinq cent ");
 break;
 case 6:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"six cents ");
 else strcat(lettre,"six cent ");
 break;
 case 7:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"sept cents ");
 else strcat(lettre,"sept cent ");
 break;
 case 8:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"huit cents ");
 else strcat(lettre,"huit cent ");
 break;
 case 9:
 if((dizaine == 0)&&(unite == 0)) strcat(lettre,"neuf cents ");
 else strcat(lettre,"neuf cent ");
 }// endSwitch(centaine)

 switch(dizaine)
 {
 case 0:
 break;
 case 1:
 if(unite==0) strcat(lettre,"dix");
 break;
 case 2:
 strcat(lettre,"vingt ");
 break;
 case 3:
 strcat(lettre,"trente ");
 break;
 case 4:
 strcat(lettre,"quarante ");
 break;
 case 5:
 strcat(lettre,"cinquante ");
 break;
 case 6:
 strcat(lettre,"soixante ");
 break;
 case 7:
 strcat (lettre,"soixante-dix ");
 break;
 case 8:
 strcat(lettre,"quatre-vingt ");
 break;
 case 9:
 strcat(lettre,"quatre-vingt-dix ");
 } // endSwitch(dizaine)

 switch(unite)
 {
 case 0:
 break;
 case 1:
 if(dizaine == 1) strcat(lettre,"onze ");
 else strcat(lettre,"un ");
 break;
 case 2:
 if(dizaine == 1) strcat(lettre,"douze ");
 else strcat(lettre,"deux ");
 break;
 case 3:
 if(dizaine == 1) strcat(lettre,"treize ");
 else strcat(lettre,"trois ");
 break;
 case 4:
 if(dizaine == 1) strcat(lettre,"quatorze ");
 else strcat(lettre,"quatre ");
 break;
 case 5:
 if(dizaine == 1) strcat(lettre,"quinze ");
 else strcat(lettre,"cinq ");
 break;
 case 6:
 if(dizaine == 1) strcat(lettre,"seize ");
 else strcat(lettre,"six ");
 break;
 case 7:
 if(dizaine == 1) strcat(lettre,"dix-sept ");
 else strcat(lettre,"sept ");
 break;
 case 8:
 if(dizaine == 1) strcat(lettre,"dix-huit ");
 else strcat(lettre,"huit ");
 break;
 case 9:
 if(dizaine == 1) strcat(lettre,"dix-neuf ");
 else strcat(lettre,"neuf ");
 } // endSwitch(unite)

 switch (i)
 {
 case 1000000000:
 if(y>1) strcat(lettre,"milliards ");
 else strcat(lettre,"milliard ");
 break;
 case 1000000:
 if(y>1) strcat(lettre,"millions ");
 else strcat(lettre,"million ");
 break;
 case 1000:
 strcat(lettre,"mille ");
 }
 } // end if(y!=0)
 reste -= y*i;
 } // end for
 //if(strlen(lettre)==0) strcpy(lettre,"zero");

 printf("le nombre est : %s\n ",lettre);
 
 }


merci d'avance
Configuration: Windows XP
Firefox 3.5.5

3 réponses

  • Votes
  • Date
  • Date décroissante
  1. reste = chiffre;

    je ne sais pas si c'est la cause de ton bug, mais cette ligne au tout début n'a pas de sens : chiffre n'est pas encore intialisé à cet endroit du programmeé.

    De plus tu utilises la valeur de reste ensuite, et donc il y a un problème à ce niveau c'est sûr.
    1. le chiffre est saisie par un printf
      printf("donner le chiffre \n");
       scanf("%d",&chiffre);

      bé oui s'est une initialisation

      franchement c une transformation de c++ à c . j'espère que tu m'aide un peut svp
      regarde un peut le programme original
      #include <iostream.h>
       #include <stdlib.h>
       #include <string.h>
       #include <conio.h>
      
       /*
       La valeur de chiffre ne doit pas dépasser la plage de valeur
       pour unsigned int càd 4 294 967 295 , sinon il y a erreur de
       conversion !!!
       */
      
       void ChiffreEnLettre(unsigned int, char*);
       int main()
       {
       unsigned int x = 0;
       char choix = 'o',c[256];
       while((choix=='o')||(choix=='O'))
       {
       cout << "Introduire un chiffre entier : ";
       cin >> x;
       ChiffreEnLettre(x,c);
       cout << "\nEn lettre ca fait : "<< c <<endl;
       cout << x;
       cout << "\nEncore d'autre? (O/N): ";
       choix = getche();
       cout << "\n==================================================="<<endl;
       }
       return 0;
       }
      
       void ChiffreEnLettre(unsigned int chiffre, char * lettre)
       {
       unsigned int centaine, dizaine, unite, reste, y;
       bool dix = false;
       strcpy(lettre, "");
       reste = chiffre;
       for(int i=1000000000; i>=1; i/=1000)
       {
       y = reste/i ;
       cout << reste <<endl; 
       cout << chiffre <<endl;
       cout << i <<endl;
       cout << y <<endl;
       if(y!=0)
       {
       centaine = y/100;
       dizaine = (y - centaine*100)/10;
       unite = y-(centaine*100)-(dizaine*10);
       switch(centaine)
       {
       case 0:
       break;
       case 1:
       strcat(lettre,"cent ");
       break;
       case 2:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"deux cents ");
       else strcat(lettre,"deux cent ");
       break;
       case 3:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"trois cents ");
       else strcat(lettre,"trois cent ");
       break;
       case 4:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"quatre cents ");
       else strcat(lettre,"quatre cent ");
       break;
       case 5:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"cinq cents ");
       else strcat(lettre,"cinq cent ");
       break;
       case 6:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"six cents ");
       else strcat(lettre,"six cent ");
       break;
       case 7:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"sept cents ");
       else strcat(lettre,"sept cent ");
       break;
       case 8:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"huit cents ");
       else strcat(lettre,"huit cent ");
       break;
       case 9:
       if((dizaine == 0)&&(unite == 0)) strcat(lettre,"neuf cents ");
       else strcat(lettre,"neuf cent ");
       }// endSwitch(centaine)
      
       switch(dizaine)
       {
       case 0:
       break;
       case 1:
       dix = true;
       break;
       case 2:
       strcat(lettre,"vingt ");
       break;
       case 3:
       strcat(lettre,"trente ");
       break;
       case 4:
       strcat(lettre,"quarante ");
       break;
       case 5:
       strcat(lettre,"cinquante ");
       break;
       case 6:
       strcat(lettre,"soixante ");
       break;
       case 7:
       dix = true;
       strcat(lettre,"soixante ");
       break;
       case 8:
       strcat(lettre,"quatre-vingt ");
       break;
       case 9:
       dix = true;
       strcat(lettre,"quatre-vingt ");
       } // endSwitch(dizaine)
      
       switch(unite)
       {
       case 0:
       if(dix) strcat(lettre,"dix ");
       break;
       case 1:
       if(dix) strcat(lettre,"onze ");
       else strcat(lettre,"un ");
       break;
       case 2:
       if(dix) strcat(lettre,"douze ");
       else strcat(lettre,"deux ");
       break;
       case 3:
       if(dix) strcat(lettre,"treize ");
       else strcat(lettre,"trois ");
       break;
       case 4:
       if(dix) strcat(lettre,"quatorze ");
       else strcat(lettre,"quatre ");
       break;
       case 5:
       if(dix) strcat(lettre,"quinze ");
       else strcat(lettre,"cinq ");
       break;
       case 6:
       if(dix) strcat(lettre,"seize ");
       else strcat(lettre,"six ");
       break;
       case 7:
       if(dix) strcat(lettre,"dix-sept ");
       else strcat(lettre,"sept ");
       break;
       case 8:
       if(dix) strcat(lettre,"dix-huit ");
       else strcat(lettre,"huit ");
       break;
       case 9:
       if(dix) strcat(lettre,"dix-neuf ");
       else strcat(lettre,"neuf ");
       } // endSwitch(unite)
      
       switch (i)
       {
       case 1000000000:
       if(y>1) strcat(lettre,"milliards ");
       else strcat(lettre,"milliard ");
       break;
       case 1000000:
       if(y>1) strcat(lettre,"millions ");
       else strcat(lettre,"million ");
       break;
       case 1000:
       strcat(lettre,"mille ");
       }
       } // end if(y!=0)
       reste -= y*i;
       dix = false;
       } // end for
       if(strlen(lettre)==0) strcpy(lettre,"zero");
       
       cout << lettre;
       }
      


      merci d'avance
      1. Le chiffre est saisie par un printf

        printf("donner le chiffre \n");
        scanf("%d",&chiffre);

        bé oui s'est une initialisation


        la variable chiffre, je veux bien, en effet elle est bien initialisée.

        Je parle de la variable reste !
        Tu lui donnes la valeur de chiffre avant que chiffre soit initialisée, ce n'est pas correct.