Results 1 to 14 of 14

Thread: Qstring bad index.

  1. #1
    Join Date
    Apr 2021
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Qstring bad index.

    Qt Code:
    1. /*qstring object/* newText = arg1;
    2.  
    3.   if(newText.size() > 0)
    4.   if(newText[newText.length()-1].isLetter())
    5.   {
    6.   for(int i{0}; i < vowels.length()-1; i++)
    7.   {
    8.   if(newText[newText.length()-1] == vowels[i])
    9.   {
    10.   newText[newText.length()-1] = vowels[i+1];
    11.   ui->lineEdit->setText(newText);
    12.   return;
    13.   }
    To copy to clipboard, switch view to plain text mode 

    vowels[i+1] indexes the last element of the Qstring, not the element one over from i. Why is this?
    Also, is there a shorter way to reference the last element of a Qstring.

  2. #2
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qstring bad index.

    What are you trying to accomplish? Is vowels a QString?

    https://doc.qt.io/qt-5/qstring.html#back

  3. #3
    Join Date
    Apr 2021
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qstring bad index.

    I am trying to overwrite the last character in newText with a specific character in vowels. Both are QString. The issue is vowels[i+1] always returns the last character of the string when it should return what is at the location of i+1.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,939
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qstring bad index.

    It would help if you gave actual examples of what "newText" and "vowels" contain.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Apr 2021
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qstring bad index.

    Both contain regular alphabetical text. Lets say that newText = "HelloWorld", and vowels = "abifedonal". Using my code, when the 'e' in hello world is matched with the 'e' in vowels, it should
    return 'd', because 'd' is contiguous to 'e'. It does not. It will always returns 'l', the last character of vowels.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,939
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qstring bad index.

    Your code never tries to match anything with the "e" in HelloWorld. It is always looking a newText[ newText.length() - 1 ], the LAST character in newText, which is a 'd' in your example.

    Qt Code:
    1. QString newText = "HelloWorld";
    2. QString vowels = "abifedonal";
    3.  
    4. int len = newText.length();
    5. if ( len > 0 )
    6. {
    7. QChar c = newText[ len - 1 ]; // contains a 'd'
    8. if ( newText[ len - 1 ].isLetter() )
    9. {
    10. for ( int i{ 0 }; i < vowels.length() - 1; i++ )
    11. {
    12. if ( newText[ len - 1 ] == vowels[ i ] )
    13. {
    14. newText[ len - 1 ] = vowels[ i + 1 ];
    15. break;
    16. }
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    When this code hits the break statement on line 15, newText contains "HelloWorlo", which is exactly what the code is supposed to do. It matches the 'd' in "HelloWorld" with the 'd' in "abifedonal" and replaces it with the next character in vowels, an 'o'.

    By the way, 'b', 'f', 'd', 'n', and 'l' are not vowels...
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qstring bad index.

    Something like this?

    Qt Code:
    1. QString vowels = "aeiouy";
    2. QString newText = "helloworld!";
    3.  
    4. for (int i = 0; i < newText.size(); ++i) {
    5. if (newText[i].isLetter()) {
    6. int vIdx = vowels.indexOf(newText[i]);
    7. if (vIdx != -1 && vIdx < vowels.size()-1) {
    8. qDebug() << vIdx << newText[i] << vowels[vIdx+1];
    9. newText[i] = vowels[vIdx+1];
    10. }
    11. }
    12. }
    13.  
    14. qDebug() << newText;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,939
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qstring bad index.

    Not quite. Your code will replace all vowels in newText, but the OP said this:

    I am trying to overwrite the last character in newText with a specific character in vowels.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qstring bad index.

    Ah true, I probably should have read that a little closer.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,939
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qstring bad index.

    And I realize that in combination with your indexOf() code, you could do it in about 2 or 3 lines.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. #11
    Join Date
    Apr 2021
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qstring bad index.

    My bad. Replace the 'd' in "HelloWorld" with 'e'.

  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,939
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qstring bad index.

    Replace the 'd' in "HelloWorld" with 'e'.
    Well, that won't happen either with your example, because 'e' comes before 'd' in your vowels string.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  13. #13
    Join Date
    Apr 2021
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qstring bad index.

    Just put one of vowels characters at the end of newtext and test it.

  14. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,939
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qstring bad index.

    The code I posted earlier does exactly that. It replaces 'd' with 'o', the next character in the vowels string after 'd'.

    Obviously the code you posted in your first post is not your real code, otherwise it would do the same.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 27th December 2013, 18:04
  2. Replies: 3
    Last Post: 27th July 2012, 09:30
  3. Replies: 2
    Last Post: 11th August 2011, 15:42
  4. Replies: 4
    Last Post: 31st January 2008, 20:44
  5. Replies: 6
    Last Post: 5th December 2007, 22:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.