Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Problems with QString at position [i] set to QChar value in a void function
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wonopon12
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maxim.prishchepa
      wrote on last edited by
      #2

      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);
      }
      }@

      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        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++)
        {
        }
        }
        @

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved