[C++] erreur

Fermé
ProgrammeurC++ - 15 juin 2008 à 17:16
Marco la baraque Messages postés 996 Date d'inscription vendredi 9 mai 2008 Statut Contributeur Dernière intervention 5 novembre 2009 - 15 juin 2008 à 19:14
Bonjour,

J'ai un problème avec un petit logiciel de compression :

#include <iostream>
#include <string.h>

using namespace std;

void compression(string phrase);

int main()
{
    string mot="CCCCCCBBBCCU333ZZZZ@YYYYYYYYYYYYY";

    cout << "Logiciel de compression" << endl << endl;

    cout << "Avant : " << mot << endl;

    compression(mot);

    return EXIT_SUCCESS;
}

void compression(string phrase)
{
    string copie="";
    int len = phrase.size();
    int i; // pour compter les boublons
    char i2[33]; // récupére int i en chaine

    for(int c=0;c<len;c++)
    {
        i=1;

        if ( phrase[c] == "@" ) { copie+="@"; } // cas particulier où il y a le flag

        else if (phrase[c]==phrase[c+1] and phrase[c]==phrase[c+2])
        {
            while( phrase[c] == phrase[c+1] ) // on avance dans la chaine et on regarde les boublons
               { i++; c++; }

            copie += itoa(i,i2,10);
            copie += "@"; // le flag
        }

        copie += phrase[c];
        copie += " ";
    }

    cout << endl << copie << endl;
}




Mais j'ai une erreur et je n'arrive pas à la régler :

error: ISO C++ forbids comparison between pointer and integer


Merci d'avance...

1 réponse

Marco la baraque Messages postés 996 Date d'inscription vendredi 9 mai 2008 Statut Contributeur Dernière intervention 5 novembre 2009 328
15 juin 2008 à 19:14
Bonsoir,
A la place de la ligne suivante :
if ( phrase[c] == "@" ) { copie+="@"; } // cas particulier où il y a le flag


il faut mettre ceci :
if ( phrase[c] == '@' ) { copie+="@"; } // cas particulier où il y a le flag


En effet, si phrase est de type string, phrase[i] (où i est entier) est de type char, et donc il faut le comparer avec un char ('@') et non pas un string ("@").

Cordialement,
0