Multiple if statements comparing text and integers - c++

I am trying to make multiple if statements with multiple conditions. If I run the code it works fine but it never changes the output. I always get the second statement >= 29. Here's my code.
if (label.text <= #"30")
{label.text = #"Text";}
else if (label.text >= #"29")
{label.text = #"Text";}
else if (label.text >= #"19")
{label.text = #"Text";}
else if (label.text >= #"10")
{label.text = #"Text";}
else if (label.text = #"00")
{label.text = #"Text";}
ok I have changed my code but i still doesn't work any suggestions
label.text = temporaryValue;
if ([label.text floatValue] <= 30)
{label.text = #"text1";}
else if ([label.text floatValue] >= 29)
{label.text = #"text2";}
else if ([label.text floatValue] >= 19)
{label.text = #"text3";}
else if ([label.text floatValue] >= 10)
{label.text = #"text4";}
else if ([label.text floatValue] == 0)
{label.text = #"text5";}

You can't compare the numerical values of a string like this; this operation performs pointer comparison, i. e. it compares the (rather random) addresses of the string instances you pass in. Use something like this:
if ([label.text floatValue] >= 30.0) {
}
etc.

Using >= or <= with NSString* would compare the addresses, not the content of those strings. If you want to compare like this, you should parse your string into an int and compare it using common integer comparison:
int val = [label.text intValue];
if (val > 30) {
...
}
else ... // more of your ifs

Once you fix your syntax (as suggested in other answers here) you will still have a problem, as once you are dealing with numeric comparisons your logic flow seems flawed. Consider this
if ([label.text floatValue] <= 30.0)
{label.text = #"Text";}
else if ([label.text floatValue] >= 29.0)
{label.text = #"Text";}
if your test value IS greater than 30, you move to the next else if
else if ([label.text floatValue] >= 29.0)
clearly this IS bigger than 29 since it failed to be less than or equal to 30. Therefore all your other conditonals are never reached as assuming your first conditional is false, the second MUST be true.

Related

Why will my elseif statment never executed

Any idea why the else if statment will be never executed ? The value of difference is constantly changing when the program runs.
double difference = abs(reale_x[0] - reale_x[1]);
if (0 <= difference < 45) {
timer_counter += 1;
if (timer_counter == 30) {
cout << "CLICK" << '\n';
}
}
else if (difference > 50) {
timer_counter = 0;
}
That is not how comparation works in c++.
What this code
if (0 <= difference < 45) {
does is it first compares if 0 is smaller or equal to difference. It is then "replaced" by a bool value either true or false. And then a bool value (so either 1 or 0) is compared to 45. And it will always be smaller than 45. What you have there is an always true statement.
So the way you would write this if statement is
if (difference >= 0 && difference < 45){
Note that because of your else if statement it will not execute if the difference is >44 and <51
if (0 <= difference < 45) will be executed as if ((0 <= difference) < 45), which will be either 0<45 or 1<45 and will always be true. That's why the else part is not getting executed.
in mathematics, we see and write 0 <= x < 45 or something like that to define the range of the variable x. But in order to tell the computer the same thing, you have to tell more clearly. Saying, to have to tell the compiler, that the value of x is greater than or equal to zero and at the same time, that value will be less than 45, and you can tell the compiler by this statement: difference >= && difference < 45 . the && is an 'AND' operator in most of the languages.

How can this if statement be simplified?

I am using CLion IDE to code my C++ project. Sometimes it happens that the IDE tries to be more intelligent than me and gives me suggestions. I have a simple problem during code inspection (by CLion). It says the following code can be simplified, even though I believe it is the most simple form I can think of :
Code :
if (node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30)
return true;
else
return false;
Assume node is of type std::pair<int, int>
The suggestion I get from the CLion IDE is the following:
Code Inspection comments :
Inspection info: This inspection finds the part of the code that can be simplified, e.g. constant conditions, identical if branches, pointless boolean expressions, etc.
Do you think this can be simplified more ?
CLion is hinting at you that this bit...
if (node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30)
return true;
else
return false;
could just be re-written as
return node.first >= 0 && node.first <= 45 &&
node.second >= 0 && node.second <= 30;
Since an expression used as a condition in a control statement obviously has a natural conversion to true and false.

how to make 2 = .01 and .01 = 2?

Okay so the title may not make the most sense but I will explain it better here.. what i'm trying to do is take the value of 2 and make it equal to .01 and make .01 = 2, well I tried to hardcode it like this
if (Value = 2)
Value = 0;
else if (Value = 1.99)
Value = .01;
else if (Value = 1.98)
Value = .02;
else if (Value = 1.97)
Value = .03;
else if (Value = 1.96)
Value = .04;
else if (Value = 1.95)
Value = .05;
(I did that all the way down to 0... however it didn't function properly
I also tried to get the inverse of the value like so..
Value = 1 / Value;
Now if the value was 2 it would return it to .5 and if the value was 1 it would set it to 1.. I knew this wouldn't work when I tried I just didn't know what to do... If anyone could lead me in the right direction that would be cool.. (also I think this thread is a little bit better than my past threads seeing how I explained it :^) )
You're using the assignment operator "=". This assigns a value to a variable (such as x = 10). Inside your if-check clause, you want to compare both variables equality with the compare-equality operator, "==".
if (Value == 2.f) // checks if Value is equal to 2
Value = 0.f; // sets Value equal to 0
else if (Value == 1.99f)
Value = .01f;
else if (Value == 1.98f)
Value = .02f;
else if (Value == 1.97f)
Value = .03f;
else if (Value == 1.96f)
Value = .04f;
else if (Value == 1.95f)
Value = .05f;
An alternative yet complete solution to your problem:
if (Value <= 2.f && Value >= 0.f) // checks if Value is between or is 2 or 0
Value = 2.f - Value; // sets !version of Value to Value
Value = 1 / Value; wouldn't work because you're trying to get the !version of Value, not the inversion of Value.
Remember to do f after a float check so you don't run into weird faulty comparison problems.

Converting c++ switch with if statement to Sql

I wrote a bunch of code in c++ to check for a multiple conditions and also I make use of case. Now am struggling to convert it sql case. Below is the c++ code
Switch(TypeEmp){
case 0:
if(Age < 65){
if((income >=0) || (income <=1880000)){
amnt= income * 52 ;
}else if(other condition){
calculate it amnt;
}
}
break;
}
Somthing like this
select ...
case
when (Age < 65) then
case
when (Income >= 0) or (Income <= 1880000) then
income * 52
when (other condition) then
--TODO: compute other condition amount - "calculate it amnt"
else
--TODO: compute amount here
end
else
--TODO: return right value here
end as amnt
...
from MyTable(s)
It'd be something like
SELECT CASE
WHEN AGE < 65 THEN
CASE
WHEN INCOME >= 0 OR INCOME <= 1880000 THEN
INCOME * 52
ELSE
CALCULATE_IT_AMNT
END
ELSE
NULL
END AS SOME_VALUE
FROM SOME_TABLE
This presumes that SOME_TABLE contains the AGE and INCOME columns used in the calculation.

Make assignment within if-statement

I have the following problem
in my app i have severeal if-statements
if ( (number >= 1 && number <= 18) && !strcmp("half1-18", _myBetCh) ) {
}
Now I realized that I have to split this condition because I need a boolean variable after one condition
bool success = false,
if(!strcmp("half1-18", _myBetCh) {
success = true;
if (number >= 1 && number <= 18) {
}
}
Is there a workaround to this? Is it possible, for instance, to make an assignment withing the if-statement?
It's possible, like this:
if ((success = !strcmp("half1-18", _myBatCh)) && number > 1 && number < 18)
but I personally think assignments in conditions are messy and hard to read, and prefer this variation:
bool success = strcmp("half1-18", _myBetCh) == 0;
if (success && number >= 1 && number <= 18) {
// ...
}
Well, there is:
if ( !strcmp("half1-18", _myBatCh) && (success = true, number > 1 && number < 18) )
or, obviating the need for the success = false earlier
if ( (success = !strcmp("half1-18", _myBatCh)) && number > 1 && number < 18 )
Your way is easier to read though, so I would consider sticking with what you've got.