IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++Builder Discussion :

Problème de convertion de chaine de caractère en int


Sujet :

C++Builder

  1. #1
    Membre actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2008
    Messages
    241
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Janvier 2008
    Messages : 241
    Points : 204
    Points
    204
    Par défaut Problème de convertion de chaine de caractère en int
    Bonjour tous le monde voilà j'ai fait le code suivant mais lors de l'exécution je reçois le message suivant invalide integer value malgré que toutes les valeurs sont des entier le code est le suivant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    int  i,j,f,min,k,t,a,r,v,h;
    int     nbv,nbcom,index1,index2,index3,ind,dr;
    int     index4,id1,id2,n,som,dk,kl,kr,so,so1,so2,lk,ap,op,index5,index6;
    int     restcap1,restcap2,nv,index7 ;
     int    vehicule[500]  ;
     int sol1[500]     ;
     int sol2 [500]     ;
    int nbrclt [500]     ;
     int capacite    [500]   ;
    int     distance [500];
     int   vehicule1[500][500]   ;
     
     
     
    nbv= StrToInt(Edit2->Text);
    nbcom=  StrToInt(Edit1->Text);
    for( index1= 1; index1<=nbv; index1++)
       distance[index1]= 0;
     for( index6= 1; index6<=nbv; index6++)
        capacite[index6]= StrToInt(cap->Cells[index6][1]) ;
     
     
     
    h= 1;
    for( v= 2; v<=StrToInt(Edit2->Text)-1; v++)
    /***  begin  { nbr vehicule*/
       f= 1;
       t= 1;
       a= 1;
       i= 1;
        s->Cells[1][v]= IntToStr(1);
    for( r= 2; r<=StrToInt(Edit1->Text); r++)
          s->Cells[r][v]= IntToStr(0);
     
     
     
     
     while( i<= StrToInt(Edit1->Text))
     if(a <= StrToInt(Edit1->Text)+1   )
       {
       t= t+1; min=  StrToInt(c->Cells[i][i]);
       for( j= 1; j<=StrToInt(Edit1->Text); j++)
         {
        k= 1;
        if((StrToInt(c->Cells[i][j])> min) &&  (StrToInt(cap->Cells[1][v])-StrToInt(d->Cells[j][1])>=0)   )
          while( k <=StrToInt(Edit1->Text))
           {
     
     
           if((StrToInt(s->Cells[k][v])== j)||( vehicule[k]==j )   )
           break;
           else
           k= k+1;
            if((k == StrToInt(Edit1->Text)+1)   )
            {
             min=  StrToInt(c->Cells[i][j]);
             f= j;
     
             }
             }
     
     
         }
           if (StrToInt(cap->Cells[v][1])-StrToInt(d->Cells[f][1])<0)
     
              goto lab1;
              else
                {
     
                s->Cells[t][v]= IntToStr(f);
                vehicule[h]= f;
                h= h+1;
                cap->Cells[v][1]=  IntToStr(StrToInt(cap->Cells[1][v])-StrToInt(d->Cells[f][1]));
                i= f;
                a= a+1;
                 }
     
     
        }
       else
       break;
        lab1:
     
       for( i= 1; i<=StrToInt(Edit1->Text); i++)
        for( j= 1;j<= StrToInt(Edit2->Text); j++)
           vehicule1[j][i]=  StrToInt(s->Cells[i][j]);
     
     
     
     
         for( index2= 1; index2<=nbv; index2++)
           {
           for( index3= 2; index3<=nbcom ; index3++)
            if(  vehicule1[index2,index3] != 0   )
               {
              distance[index2]= distance[index2]+StrToInt(c->Cells[vehicule1[index2][index3-1]][vehicule1[index2][index3]]);
              ind= index3;
              }
            distance[index2]= distance[index2]+StrToInt(c->Cells[vehicule1[index2][1]][vehicule1[index2][ind]]);
            nbrclt[index2]= ind-1;
     
            }
     
     
     
     
     
     
    //////////////////////////////////////////////////////////////////////////////
           for( index4= 1; index4<=nbv; index4++)
            {
               if( nbrclt[index4]>=4   )
                 {
                 id1= 2  ;
                 while( id1< nbrclt[index4])
                  {
                  for( id2=  id1+1; id2<=nbrclt[index4]-1; id2++)
                    {
                        for( kr=  id1; kr<=((id1+id2 )/  2); kr++)
                         {
                        n= vehicule1[index4][kr];
                        vehicule1[index4][kr]=  vehicule1[index4][(id1+id2-1)-kr+1] ;
                        vehicule1[index4][(id1+id2-1)-kr+1] =  n ;
                         }
                         som= StrToInt(c->Cells[vehicule1[index4][1]][vehicule1[index4][nbrclt[index4]+1]]);
                         for( dk= 1; dk<=nbrclt[index4]; dk++)
                           {
                           som= som + StrToInt(c->Cells[vehicule1[index4][dk]][vehicule1[index4][dk+1]]);
                           }
                      if(distance[index4] < som    )
                         distance[index4]=  som  ;
                       else
                          for( kl=  id1; kl<=((id1+id2 )/  2); kl++)
                           {
                          n= vehicule1[index4][kl];
                          vehicule1[index4][kl]=  vehicule1[index4][(id1+id2-1)-kl+1] ;
                          vehicule1[index4][(id1+id2-1)-kl+1] =  n ;
                           }
                   }
                id1= id1+1;
     
               }
     
              }
          }
    /////////////////////////////////////////////////////////////////////////////
     
     
         nv= 0;
          for( index7= 1; index7<=nbv; index7++)
           if(vehicule1[index7][2]!= 1   )
              nv= nv+1;
     
     
     
     
     
     
     
     
     for( op= 1; op<=nv; op++)
      for( ap= op+1; ap<=nv; ap++)
        {
     
        if(op !=ap   )
         {
        for( index1= 1; index1<=nbcom; index1++)
          {
         sol1[index1]=  vehicule1[op][index1];
         sol2[index1]=  vehicule1[ap][index1];
     
          }
     
         for( index4= 2; index4<=nbrclt[op]+1; index4++)
         for( index3= 2; index3<=nbrclt[ap]+1; index3++)
           {
     
           h=  sol1[index4];
           sol1[index4]= sol2[index3];
           sol2[index3]= h;
           so1= StrToInt(c->Cells[sol1[1]][ sol1[nbrclt[op]+1]]);
           so2= StrToInt(c->Cells[sol2[1]][ sol2[nbrclt[ap]+1]]);
     
                for( kl= 1; kl<=nbrclt[op]; kl++)
                   {
                   so1 = so1+ StrToInt(c->Cells[sol1[kl]][sol1[kl+1]]);
                   }
                for( lk= 1; lk<=nbrclt[ap]; lk++)
                   {
                   so2 = so2+ StrToInt(c->Cells[sol2[lk]][sol2[lk+1]]);
                   }
     
     
          if((so1+so2 > distance[op]+distance[ap])   )
            {
     
                 restcap1= capacite[op];
                 restcap2= capacite[ap];
     
     
                for( index1= 2; index1<=nbcom; index1++)
                   {
                   if((sol1[index1]!= 0)   )
                    restcap1= restcap1- StrToInt(d->Cells[sol1[index1]][1]) ;
     
                  if((sol2[index1]!= 0)   )
                     restcap2= restcap2-StrToInt(d->Cells[sol2[index1]][1]) ;
                     }
     
                 if((restcap1>=0)&& (restcap2>=0)   )
                   {
                    distance[op]=  so1;
                    distance[ap]=  so2;
                     for( index5=  1; index5<=nbcom; index5++)
                        {
                        vehicule1[op][index5]=   sol1[index5] ;
                        vehicule1[ap][index5]=   sol2[index5] ;
                        }
                   }
           }
           else
             {
            h=  sol2[index3];
            sol2[index3]= sol1[index4];
            sol1[index4]= h;
             }
           }
     
         }
        }
     
     
     
     
     
      for( i= 1; i<=StrToInt(Edit1->Text); i++)
        for( j= 1;j<= StrToInt(Edit2->Text); j++)
         s->Cells[i][j]=  IntToStr(vehicule1[j][i]);
     
     
     
     
     
     
     
    }

  2. #2
    Responsable Magazine

    Avatar de pottiez
    Homme Profil pro
    Développeur C++
    Inscrit en
    Novembre 2005
    Messages
    7 152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur C++
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2005
    Messages : 7 152
    Points : 22 087
    Points
    22 087
    Par défaut
    Il n'y a qu'une chose qui peut produire cette erreur dans ton code, c'est les lignes avec StrToInt.
    Tu doit avoir, lors de l'exécution de ton code au moins un des champs qui ne comporte pas que des chiffres, soit dans le Edit1, soit dans le Edit2, soit dans ta StringGrid, à savoir qu'un champ vide n'est pas considéré comme valide pour la fonction StrToInt, tu dois avoir absolument un chiffre.
    Quand une réponse vous a été utile, pensez à utiliser le nouveau système de notation

    Lisez le magazine de developpez.com.
    Mes tutos : http://pottiez.developpez.com

    La FAQ BCB -> 642 Questions/Réponses, si vous voulez participer, contactez moi. Aide de BCB6 en français

    N'oubliez pas que l'aide existe et est affichée sous simple pression de la touche F1 , une touche c'est plus rapide que tout un message .

  3. #3
    Membre éprouvé Avatar de b_reda31
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2007
    Messages
    899
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2007
    Messages : 899
    Points : 961
    Points
    961
    Par défaut
    Mieux vaut utiliser StrToIntDef afin d"éviter ce genre de problème.
    « Il est assez difficile de trouver une erreur dans son code quand on la cherche. C’est encore bien plus dur quand on est convaincu que le code est juste!!»

Discussions similaires

  1. [Perl] Problème de remplacement de chaine de caractères
    Par LE NEINDRE dans le forum Langage
    Réponses: 3
    Dernier message: 17/10/2006, 09h28
  2. Réponses: 2
    Dernier message: 10/05/2006, 15h23
  3. Réponses: 2
    Dernier message: 23/03/2006, 12h39
  4. Réponses: 11
    Dernier message: 22/03/2006, 21h57
  5. Réponses: 4
    Dernier message: 20/08/2004, 10h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo