Problems with QString at position [i] set to QChar value in a void function



  • Hey, I'm having a bit of a problem in this code. The problem is in the code within the if statement brackets. However, the problem only occurs with the givenphonenum[i] = QChar(// number //) parts. The if statements are working fine, I tested with different code in the brackets. This code also works when not in a function for some reason. I appreciate all your help everyone, thank you.



  • Try that:
    @void Convert(QString givenphonenum){
    int replaceCount = 9;
    QHash<int, QString> dictionary;
    for(int i = 2; i <= replaceCount; ++i){
    switch(i){
    case 2: dictionary.insert(i, "abc"); break;
    case 3: dictionary.insert(i, "def"); break;
    case 4: dictionary.insert(i, "ghi"); break;
    case 5: dictionary.insert(i, "jkl"); break;
    case 6: dictionary.insert(i, "mno"); break;
    case 7: dictionary.insert(i, "pqr"); break;
    case 8: dictionary.insert(i, "tuv"); break;
    case 9: dictionary.insert(i, "wxyz"); break;
    }
    QHashIterator<int, QString> iter;
    while(iter.hasNext()){
    iter.next();
    QString reg = QString("[%1]").arg(iter.value());
    QString replaceTo = QString::number(iter.key());
    givenphonenum = givenphonenum.replace(QRegExp(reg, Qt::CaseInsensitive), replaceTo);
    }
    }@



  • Hi,

    first of all, I moved it to the C++ forum, as it's more a generic problem then a Qt problem.

    I assume, you cxall the function with a string and then (outside the function) the string is not changed, right?

    Zhis comes due to the fact, that cou call the function with a copy of your string you want to change. If you want to modify the original string, you need a pointer or a reference:

    @
    void Convert(QString& givenphonenum)
    {
    }
    @

    The second problem you have, is that the string might be shorter then 100 chars. Try out the following:

    @
    void Convert(QString& givenphonenum)
    {
    for (int i = 0; i < givenphonenum.length(); i++)
    {
    }
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.