Operations with QString and int
-
wrote on 1 Mar 2022, 09:07 last edited by
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!
-
wrote on 2 Mar 2022, 04:31 last edited by
@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
-
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!
Lifetime Qt Championwrote on 1 Mar 2022, 09:14 last edited by jsulm 3 Jan 2022, 09:20@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-5dAlso, think about what should happen if last number is 9?
-
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!
wrote on 1 Mar 2022, 09:19 last edited by KroMignon 3 Jan 2022, 09:19@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.
-
@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.
wrote on 1 Mar 2022, 09:41 last edited by@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. -
@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.wrote on 1 Mar 2022, 09:48 last edited by KroMignon 3 Jan 2022, 09:48@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. -
@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-5dAlso, think about what should happen if last number is 9?
wrote on 1 Mar 2022, 09:52 last edited byQString 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-
Though it's not the desired output , yet I loved the appearance ofSpade
from nowhere! -
@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.wrote on 1 Mar 2022, 09:53 last edited by@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.
-
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-
Though it's not the desired output , yet I loved the appearance ofSpade
from nowhere!what's wrong with
QString myStr = "31415"; int myInt = myStr.toInt(); myStr = QString::number(++myInt);
?
-
@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-5dAlso, think about what should happen if last number is 9?
wrote on 1 Mar 2022, 09:55 last edited by@jsulm 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.
-
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-
Though it's not the desired output , yet I loved the appearance ofSpade
from nowhere!wrote on 1 Mar 2022, 09:56 last edited by KroMignon 3 Jan 2022, 09:59@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'
-
what's wrong with
QString myStr = "31415"; int myInt = myStr.toInt(); myStr = QString::number(++myInt);
?
wrote on 1 Mar 2022, 10:01 last edited by@J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"
-
@J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"
wrote on 1 Mar 2022, 10:04 last edited by KroMignon 3 Jan 2022, 10:05@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. -
@J-Hilk Yes, it works absolutely fine. I was trying this with real values say - myStr = " 314.19"
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"); }
-
wrote on 1 Mar 2022, 15:19 last edited by
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.
-
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.
wrote on 2 Mar 2022, 02:26 last edited by@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.
-
@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.wrote on 2 Mar 2022, 02:28 last edited by@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++] -
@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'
wrote on 2 Mar 2022, 02:50 last edited by@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;
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 .
-
wrote on 2 Mar 2022, 04:31 last edited by
@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
-
@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++]wrote on 2 Mar 2022, 07:10 last edited by@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. -
@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
wrote on 2 Mar 2022, 09:45 last edited by Swati777999 3 Feb 2022, 09:52@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;
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;
1/24