Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Operations with QString and int
QtWS25 Last Chance

Operations with QString and int

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 7 Posters 1.3k 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.
  • Swati777999S Swati777999

    I have written the following code to increase the last digit of the QString by 1

    QString myStr = "31415";
    int myStr_sz = myStr.size();
    int last_dig=myStr[myStr_sz-1].digitValue();
    last_dig+=1;
    debug()<<"New String is =" <<myStr;
    

    This has not modifed myStr . Can someone tell me where I am going wrong?

    Thanks in advance!

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #3

    @Swati777999 said in Operations with QString and int:

    This has not modifed myStr . Can someone tell me where I am going wrong?

    Qt is a C++ framework, so please take time to learn some C++ basics.
    It looks to me as you are expected Qt/C++ working as another language like Python or JavaScript.
    But that's not the case, and will never be!

    Each programming language has his own way to work, so please learn C++ if you want to use Qt or maybe PySide (Qt binding for Python) is a better alternative for you.

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    Swati777999S 1 Reply Last reply
    1
    • KroMignonK KroMignon

      @Swati777999 said in Operations with QString and int:

      This has not modifed myStr . Can someone tell me where I am going wrong?

      Qt is a C++ framework, so please take time to learn some C++ basics.
      It looks to me as you are expected Qt/C++ working as another language like Python or JavaScript.
      But that's not the case, and will never be!

      Each programming language has his own way to work, so please learn C++ if you want to use Qt or maybe PySide (Qt binding for Python) is a better alternative for you.

      Swati777999S Offline
      Swati777999S Offline
      Swati777999
      wrote on last edited by
      #4

      @KroMignon
      Thanks for your suggestion. I was working in Python before starting to work in Qt, even though I have studied c++ before, I feel my reasoning is biased towards Python's logic and syntax.

      “ In order to be irreplaceable, one must always be different” – Coco Chanel

      KroMignonK 1 Reply Last reply
      0
      • Swati777999S Swati777999

        @KroMignon
        Thanks for your suggestion. I was working in Python before starting to work in Qt, even though I have studied c++ before, I feel my reasoning is biased towards Python's logic and syntax.

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #5

        @Swati777999 said in Operations with QString and int:

        I was working in Python before starting to work in Qt, even though I have studied c++ before

        Again Qt is not a programming language, Qt is a framework developped in C++.
        Like django is a web framework developped in Python.

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        Swati777999S 1 Reply Last reply
        3
        • jsulmJ jsulm

          @Swati777999 said in Operations with QString and int:

          This has not modifed myStr

          Why should it? You do not modify it.
          You also need to replace the last character in myStr with the new number.
          Use https://doc.qt.io/qt-5/qstring.html#operator-5b-5d

          Also, think about what should happen if last number is 9?

          Swati777999S Offline
          Swati777999S Offline
          Swati777999
          wrote on last edited by
          #6

          @jsulm

          QString myStr = "31415";
           int myStr_sz = myStr.size();
           int last_dig=myStr[myStr_sz-1].digitValue();
           last_dig+=1;
           myStr[myStr_sz-1] = last_dig;
           qDebug()<<"New String is =" <<myStr;
          

          With this modified code , I got the following result-
          last_dig_incr.png
          Though it's not the desired output , yet I loved the appearance of Spade from nowhere!

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          J.HilkJ KroMignonK 2 Replies Last reply
          0
          • KroMignonK KroMignon

            @Swati777999 said in Operations with QString and int:

            I was working in Python before starting to work in Qt, even though I have studied c++ before

            Again Qt is not a programming language, Qt is a framework developped in C++.
            Like django is a web framework developped in Python.

            Swati777999S Offline
            Swati777999S Offline
            Swati777999
            wrote on last edited by
            #7

            @KroMignon Yes, I am acquainted with the fact that Qt is the framework of C++ but I struggle to work with its classes and functions.

            “ In order to be irreplaceable, one must always be different” – Coco Chanel

            1 Reply Last reply
            0
            • Swati777999S Swati777999

              @jsulm

              QString myStr = "31415";
               int myStr_sz = myStr.size();
               int last_dig=myStr[myStr_sz-1].digitValue();
               last_dig+=1;
               myStr[myStr_sz-1] = last_dig;
               qDebug()<<"New String is =" <<myStr;
              

              With this modified code , I got the following result-
              last_dig_incr.png
              Though it's not the desired output , yet I loved the appearance of Spade from nowhere!

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #8

              @Swati777999

              what's wrong with

              QString myStr = "31415";
              int myInt = myStr.toInt();
              myStr = QString::number(++myInt);
              

              ?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              Swati777999S 1 Reply Last reply
              4
              • jsulmJ jsulm

                @Swati777999 said in Operations with QString and int:

                This has not modifed myStr

                Why should it? You do not modify it.
                You also need to replace the last character in myStr with the new number.
                Use https://doc.qt.io/qt-5/qstring.html#operator-5b-5d

                Also, think about what should happen if last number is 9?

                Swati777999S Offline
                Swati777999S Offline
                Swati777999
                wrote on last edited by
                #9

                @jsulm said in Operations with QString and int:

                @Swati777999 said in Operations with QString and int:

                Also, think about what should happen if last number is 9?

                Yes, this is in my mind, it's the next stage of this trial but before that, I need to be sure about the working of incrementation.

                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                1 Reply Last reply
                0
                • Swati777999S Swati777999

                  @jsulm

                  QString myStr = "31415";
                   int myStr_sz = myStr.size();
                   int last_dig=myStr[myStr_sz-1].digitValue();
                   last_dig+=1;
                   myStr[myStr_sz-1] = last_dig;
                   qDebug()<<"New String is =" <<myStr;
                  

                  With this modified code , I got the following result-
                  last_dig_incr.png
                  Though it's not the desired output , yet I loved the appearance of Spade from nowhere!

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by KroMignon
                  #10

                  @Swati777999 said in Operations with QString and int:

                  With this modified code , I got the following result-

                  It is doing what you have coded.

                  QString myStr = "31415";
                   int myStr_sz = myStr.size(); // => myStr = 5
                   int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5
                   last_dig+=1; /// => last_dig = 6
                   myStr[myStr_sz-1] = last_dig;  // change  myStr[myStr_sz-1] from '5' to '\x06'
                   qDebug()<<"New String is =" <<myStr;
                  

                  EDIT: you have to understand that 5 != '5', the ASCII code for 5 is 57 or '\x35'

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  Swati777999S 1 Reply Last reply
                  3
                  • J.HilkJ J.Hilk

                    @Swati777999

                    what's wrong with

                    QString myStr = "31415";
                    int myInt = myStr.toInt();
                    myStr = QString::number(++myInt);
                    

                    ?

                    Swati777999S Offline
                    Swati777999S Offline
                    Swati777999
                    wrote on last edited by
                    #11

                    @J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

                    “ In order to be irreplaceable, one must always be different” – Coco Chanel

                    KroMignonK J.HilkJ 2 Replies Last reply
                    0
                    • Swati777999S Swati777999

                      @J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by KroMignon
                      #12

                      @Swati777999 said in Operations with QString and int:

                      Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

                      Please again, don't program in C++ as you would do it in python, this will not work!
                      There is no implicit convertion from string <=> real or string <=> integer in C++.
                      I you want to program in python style, use python not C++ or you will only go frustrated.

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      Swati777999S 1 Reply Last reply
                      0
                      • Swati777999S Swati777999

                        @J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by J.Hilk
                        #13

                        @Swati777999

                        template<typename T>
                        QString addOne(const QString &string)
                        {
                            auto value = std::is_floating_point<T>::value ? string.toDouble() : string.toInt();
                            return QString::number(++value);
                        }
                        
                        int main(int argc, char *argv[])
                        {
                            qDebug() << addOne<int>("12345") << addOne<float>("1234.56");
                        }
                        

                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        4
                        • M Offline
                          M Offline
                          mchinand
                          wrote on last edited by
                          #14

                          It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.

                          Swati777999S 1 Reply Last reply
                          1
                          • M mchinand

                            It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.

                            Swati777999S Offline
                            Swati777999S Offline
                            Swati777999
                            wrote on last edited by
                            #15

                            @mchinand said in Operations with QString and int:

                            It's not entirely clear, but from the wording of the original post (increase the last digit by one) and then in a subsequent message saying it should work with strings that contain floating-point numbers too, I think @Swati777999 wants '35.456' to become '35.457', for example.

                            Yes, actually my true intention for putting this Question was about dealing with rounding of real values but I also wanted to be clear about the string and int conversion.

                            “ In order to be irreplaceable, one must always be different” – Coco Chanel

                            1 Reply Last reply
                            0
                            • KroMignonK KroMignon

                              @Swati777999 said in Operations with QString and int:

                              Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"

                              Please again, don't program in C++ as you would do it in python, this will not work!
                              There is no implicit convertion from string <=> real or string <=> integer in C++.
                              I you want to program in python style, use python not C++ or you will only go frustrated.

                              Swati777999S Offline
                              Swati777999S Offline
                              Swati777999
                              wrote on last edited by
                              #16

                              @KroMignon
                              My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]

                              “ In order to be irreplaceable, one must always be different” – Coco Chanel

                              KroMignonK 1 Reply Last reply
                              0
                              • KroMignonK KroMignon

                                @Swati777999 said in Operations with QString and int:

                                With this modified code , I got the following result-

                                It is doing what you have coded.

                                QString myStr = "31415";
                                 int myStr_sz = myStr.size(); // => myStr = 5
                                 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5
                                 last_dig+=1; /// => last_dig = 6
                                 myStr[myStr_sz-1] = last_dig;  // change  myStr[myStr_sz-1] from '5' to '\x06'
                                 qDebug()<<"New String is =" <<myStr;
                                

                                EDIT: you have to understand that 5 != '5', the ASCII code for 5 is 57 or '\x35'

                                Swati777999S Offline
                                Swati777999S Offline
                                Swati777999
                                wrote on last edited by
                                #17

                                @KroMignon said in Operations with QString and int:

                                @Swati777999 said in Operations with QString and int:

                                With this modified code , I got the following result-

                                It is doing what you have coded.

                                QString myStr = "31415";
                                 int myStr_sz = myStr.size(); // => myStr = 5
                                 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5
                                 last_dig+=1; /// => last_dig = 6
                                 myStr[myStr_sz-1] = last_dig;  // change  myStr[myStr_sz-1] from '5' to '\x06'
                                 qDebug()<<"New String is =" <<myStr;
                                

                                EDIT: you have to understand that 5 != '5', the ASCII code for 5 is 57 or '\x35'

                                EXAMPLE-1

                                int x = 56;
                                  QString str = QString::number(x);
                                  qDebug()<<"x = " <<str; // x ="56"
                                

                                Using Example 1 -
                                I modified the code

                                 ```
                                 QString myStr = "31415";
                                 qDebug()<<"old String is =" <<myStr;  //31415
                                 int myStr_sz = myStr.size(); // => myStr = 5
                                 int last_dig=myStr[myStr_sz-1].digitValue(); // => last_dig = 5
                                 last_dig+=1; // => last_dig = 6
                                 qDebug()<<"incremented last dig is="<<last_dig;  // 6
                                 QString str_last =QString::number(last_dig);  
                                 qDebug()<<"incremented last dig after converting to string is="<<last_dig; // ==> 6
                                 myStr[myStr_sz-1] = last_dig; 
                                 qDebug()<<"New String is =" <<myStr;
                                

                                trial-2_02.03.2022.png

                                I am getting the same old result.

                                Another way I'm thinking is that, popping out the last digit from the string and appending the incremented last digit . I tried it but didn't work. Please give suggestion .

                                “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Sivan
                                  wrote on last edited by
                                  #18

                                  @Swati777999 said in Operations with QString and int:

                                  You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                                   myStr[myStr_sz-1] = last_dig + '0'; 
                                  

                                  But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                                  Swati777999S 2 Replies Last reply
                                  1
                                  • Swati777999S Swati777999

                                    @KroMignon
                                    My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]

                                    KroMignonK Offline
                                    KroMignonK Offline
                                    KroMignon
                                    wrote on last edited by
                                    #19

                                    @Swati777999 said in Operations with QString and int:

                                    My current project requirement expects me to code in Qt C++ [ As I've mentioned earlier that my mind always approaches the solution of a program in Python first because of spending more time in coding in Python than C++]

                                    So we are totally in opposite about the way to do: programming C++ but thinking the program with Python in mind is, for me, the worst way to do.

                                    C++ and python are so far apart in almost every aspect of programming, I can't see how this can work.
                                    If we are only talking about validating an algorithm, why not. But transposing from Python to C++ is not a simple matter. That alone amounts to a new development.

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    1 Reply Last reply
                                    3
                                    • S Sivan

                                      @Swati777999 said in Operations with QString and int:

                                      You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                                       myStr[myStr_sz-1] = last_dig + '0'; 
                                      

                                      But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                                      Swati777999S Offline
                                      Swati777999S Offline
                                      Swati777999
                                      wrote on last edited by Swati777999
                                      #20

                                      @Sivan said in Operations with QString and int:

                                      @Swati777999 said in Operations with QString and int:

                                      You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                                       myStr[myStr_sz-1] = last_dig + '0'; 
                                      

                                      But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                                      Yes, your suggestion works perfectly well.

                                          QString myStr = "31415";
                                          qDebug()<<"old String is =" <<myStr;
                                          int myStr_int = myStr.toInt();
                                          myStr_int +=1;
                                          qDebug()<<"New int value is =" <<myStr_int;
                                      

                                      trial-6(a)_02.03.png

                                      Trying with real vlaues

                                          QString myStr = "99.99";
                                          qDebug()<<"old String is =" <<myStr;
                                          int myStr_int = myStr.toDouble();   //  And:100  [.toFloat() ]
                                          myStr_int +=1;
                                          qDebug()<<"New int value is =" <<myStr_int;
                                      

                                      “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                      1 Reply Last reply
                                      0
                                      • S Sivan

                                        @Swati777999 said in Operations with QString and int:

                                        You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                                         myStr[myStr_sz-1] = last_dig + '0'; 
                                        

                                        But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                                        Swati777999S Offline
                                        Swati777999S Offline
                                        Swati777999
                                        wrote on last edited by
                                        #21

                                        @Sivan said in Operations with QString and int:

                                        @Swati777999 said in Operations with QString and int:

                                        You are assigning ASCII value of 6 to the last character of string. Please refer to ASCII chart online. To simply answer your question, you can add '0' to ur last_dig. That should work.

                                         myStr[myStr_sz-1] = last_dig + '0'; 
                                        

                                        But why are u doing like this? What if the last digit is 9? When u add 1, you will need to add the second last digit as well. A better solution would be to completely convert the string to digit, do the arithmetric operation, and then convert it back to QString

                                        Here's my try with real values

                                          QString myStr = "99.20";
                                            qDebug()<<"old String is =" <<myStr;
                                            int indx = myStr.indexOf(".");
                                            myStr.remove('.');
                                            float myStr_int = myStr.toFloat();   //100  
                                            myStr_int +=1;
                                            QString myStrNew = QString::number(myStr_int);
                                            myStrNew.insert(indx,".");
                                            qDebug()<<"New int value is =" <<myStrNew.toFloat();
                                        

                                        trial-6(b)_02.03.png

                                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                                        1 Reply Last reply
                                        0
                                        • Christian EhrlicherC Offline
                                          Christian EhrlicherC Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #22

                                          The output 99.21 is correct don't know why you do such strange stuff just to add 0.01 to a float value though)

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          Swati777999S 1 Reply Last reply
                                          1

                                          • Login

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