How mathematical operations done correctly in qtCreator?
-
Hi everyone;
when I put the from GUI my gap is 400 at this my first if condition should execute but in current code my all if conditions executing why?gap=((y)-(x)); qDebug()<<"gap:"<<gap; if(gap<=448){ if(392<=gap<=448) { QProcess p1 ; p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh"); } if (336<=gap<=392) { QProcess p2 ; p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh"); } if (280<=gap<=336) { QProcess p3 ; p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh"); } if (224<=gap<=280) { QProcess p4 ; p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh"); } if (168<=gap<=280) { QProcess p5 ; p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh"); } } }
thanks
-
@Wasee First: this has nothing to do with Qt or QtCreator. This is pure C++.
Did you verify that gap is in fact 400?
What does your qDebug print?
How often is this code executed?
How did you verify that all if conditions are executed? -
@Wasee said in How mathematical operations done correctly in qtCreator?:
if(392<=gap<=448)
And to add @jsulm - this is not what you think it is doing...
-
@jsulm Hi;
Thanks to your valuable reply!- Yes I verify my gap value is correct
- qDebug to see the gap value
- Code is executed when gap value x and y value will be inserted
- All conditions are executed even with no effect of gap value
I want below all if conditions should be dependent on gap value now which is not.
thanks -
@Wasee said in How mathematical operations done correctly in qtCreator?:
if(gap<=448){
if(392<=gap<=448)
{
QProcess p1 ;
p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh");
}
if (336<=gap<=392)
{
QProcess p2 ;
p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh");
}
if (280<=gap<=336)
{
QProcess p3 ;
p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh");
}
if (224<=gap<=280)
{
QProcess p4 ;
p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh");
}
if (168<=gap<=280)
{
QProcess p5 ;
p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh");
}What is this? this not valid C++ code.
You should change your code to something like:
if(gap<=448){ if(gap >= 392) { QProcess p1 ; p1.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/echo_example.sh"); } else if (gap >= 336) { QProcess p2 ; p2.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/comment_example.sh"); } else if (gap >= 280) { QProcess p3 ; p3.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/for_example.sh"); } else if (gap >= 224) { QProcess p4 ; p4.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/multiline_example.sh"); } else if (gap >= 168) { QProcess p5 ; p5.startDetached("/bin/bash", QStringList() << "-c" << "/home/ijaz/bashScripts/script.sh"); } }
-
@KroMignon now that we're on the topic of correcting code in general let me change it so, that it doesn't hurt my eyes :P
if(gap <= 448 && gab >= 168) { QStringList args ("-c"); if(gap >= 392) { args << "/home/ijaz/bashScripts/echo_example.sh"; } else if (gap >= 336) { args << "/home/ijaz/bashScripts/comment_example.sh"); } else if (gap >= 280) { args << "/home/ijaz/bashScripts/for_example.sh"); } else if (gap >= 224) { args << "/home/ijaz/bashScripts/multiline_example.sh"); } else { args << "/home/ijaz/bashScripts/script.sh"); } Process::startDetached("/bin/bash", args); }
-
@J-Hilk said in How mathematical operations done correctly in qtCreator?:
now that we're on the topic of correcting code in general let me change it so, that it doesn't hurt my eyes :P
This was not my idea to give to "cleanest" code, it was just about the test, which were wrong.
And yes, the start code and my little correction are horrible ;) -
@jsulm said in How mathematical operations done correctly in qtCreator?:
Valid C++ syntax (which you should learn) is:
if(392 <= gap && gap <= 448)
Maybe we can help him improve on his C++ skills: If you write
if(392<=gap<=448)
what C++ does is first evaluate392<=gap
which istrue
. It looks liketrue
is then implicitly casted to anint
. Thisint
is now1
and that is compared to448
. Since in all your conditionals 400 is larger than your lower bound, you always compare 1 to the upper bound. That is the entire reason why all conditions are executed.The compiler might even optimize your checks away because it can figure out that you are always comparing either
true
orfalse
to the upper bound. And in every condition both 1 and 0 are less than your upper bounds. -
Hi everyone!
I am adding/subtracting two string values but giving me wrong behavior.QString t="400000000"; QString p="0x208050"; QString result = t+p; QString result=t-p; qDebug()<<"Value:"<<result;
Value: "4000000000x208050" for adding
value: error for subtraction no matching operator - operands are strings -
@Wasee said in How mathematical operations done correctly in qtCreator?:
I am adding/subtracting two string values
You can not add/substract string, only numbers... see QString::toInt() and similar QString functions.