I am using Qt Creator to develop a C++ application and the debugger to examine the code, I am trying to understand some very odd results reported by the debugger.
if ( intDelimiter == -1
&& (intOpB = strProcessed.indexOf("[")) >= 0
&& (intClB = strProcessed.indexOf("]", ++intOpB) >= 0) ) {
strRef = strProcessed.mid(intOpB, intClB - intOpB);
if ( pobjNode != NULL ) {
strProcessed.replace(strRef, pobjNode->strGetAttr(strRef));
}
I have a breakpoint on the line:
strRef = strProcessed.mid(intOpB, intClB - intOpB);
In the code snippet above strProcessed contains:
"1079-[height]"
When the breakpoint is hit, intClB contains 1 and intOpB contains 6.
intOpB is correct because the returned value from indexOf is 5 then its incremented before the search for "]", but intClB is not correct, why is the debugger reporting it as 1? This makes no sense to me.
I am using:
Qt Creator 3.6.0
Based on Qt 5.5.1 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64bit)
Built On Dec 15 2015 01:01:12
Revision: b52c2f91f5
As spotted by king_nak, the corrected code should read:
if ( intDelimiter == -1
&& ((intOpB = strProcessed.indexOf("[")) >= 0
&& (intClB = strProcessed.indexOf("]", ++intOpB)) >= 0) ) {
strRef = strProcessed.mid(intOpB, intClB - intOpB);
if ( pobjNode != NULL ) {
strProcessed.replace(strRef, pobjNode->strGetAttr(strRef));
}
}
You have misplaced a brace:
(intClB = strProcessed.indexOf("]", ++intOpB) >= 0)
This assigns the result of strProcessed.indexOf("]", ++intOpB) >= 0 to intClB, interpreted as int. As this statement is true, intClB = 1.
You want:
(intClB = strProcessed.indexOf("]", ++intOpB) ) >= 0
^ Brace here
Related
Only the initial if statement variable computation is being completed--the ELSE part is being ignored. Can someone explain why? Many thanks.
DATASET ACTIVATE DataSet1.
DO IF ((A1_SCN2_PR1_UE = 0 & A1_SCN3_PR1_UE = 0 & A1_SCN4_PR1_UE = 0 & A1_SCN5_PR1_UE = 0) |
(A2_SCN2_PR1_UE = 0 & A2_SCN3_PR1_UE = 0 & A2_SCN4_PR1_UE = 0 & A2_SCN5_PR1_UE = 0) |
(A3_SCN2_PR1_UE = 0 & A3_SCN3_PR1_UE = 0 & A3_SCN4_PR1_UE = 0 & A3_SCN5_PR1_UE = 0)).
Compute FM_zero = 1.
ELSE.
Compute FM_zero = 0.
End IF.
EXECUTE.
Not sure why your ELSE is not being computed, but I suggest you drop the DO IF and go this way instead:
compute FM_zero =
((A1_SCN2_PR1_UE = 0 & A1_SCN3_PR1_UE = 0 & A1_SCN4_PR1_UE = 0 & A1_SCN5_PR1_UE = 0) |
(A2_SCN2_PR1_UE = 0 & A2_SCN3_PR1_UE = 0 & A2_SCN4_PR1_UE = 0 & A2_SCN5_PR1_UE = 0) |
(A3_SCN2_PR1_UE = 0 & A3_SCN3_PR1_UE = 0 & A3_SCN4_PR1_UE = 0 & A3_SCN5_PR1_UE = 0)).
This will put a 1 in all true cases and 0 in all false cases.
SPSS uses three-valued logic: True, False, or don't know (sysmis).
From the Syntax Reference Manual...
Missing values returned by the logical expression on DO IF or on any ELSE IF cause control to pass to
the END IF command at that point.
So generally you should put the sysmis test first in your DO IF and follow with appropriate ELSE IF/ELSE.
if(10) it is true, but if(10 == true) is false. Can someone tell me why the first case convert the number to bool but the second case didnt?
if (10) is equivalent to if (10 != 0), whereas if (10 == true) is if (10 == 1) (since true is promoted to the value 1 of type int).
In layman's terms: Two things that both happen to satisfy some property aren't automatically the same thing.
(E.g. doughnuts and frisbees are both round, but that doesn't mean a doughnut is equal to a frisbee. Integers and booleans can both be evaluated in a boolean context, but that doesn't mean that every integer that evaluates as true is equal to every true boolean.)
if( ... )
{
// if statement
}
To execute if statement in C++, ... should have a true value.
When you wrote if( 10 ){//something useful}
I think 10 treats as int but not bool variable. The following logic should be applied then
if( 10 ) -> if( bool( 10 ) ) -> if( true )
When you write if( 10 == true ){//something useful}, according to C++ standard, there should be the following logic behind the scene
if( 10 == true ) -> if( 10 == int( true ) ) -> if( 10 == 1 ) -> if( false )
You may write something like
if( 10 != false )
or
if( !!10 == true )
also
if( ( bool ) 10 == true ) // alternatively, if( bool ( 10 ) == true )
In old C (before C99), there is no false or true, but there are 0 or non-0 values.
In modern C (from C99), there is false or true (<stdbool.h>), but they are syntactic sugar for 0 and 1, respectively.
if( 10 ) // evaluates directly since 10 is non-zero value
if( 10 == true ) -> if( 10 == 1 ) -> if( 0 )
Because they are entirely different things.
In C, anything that is NOT false is automatically true, and C has a very strict definition of false.
You can think of if(10) as if(10 != false)
and likewise if (10 == true) as if((10 == true) != false)
10 is clearly not true, in the sense that 10 is a number and true is a boolean.
When you're in an if statement, the compiler has to evaluate the condition until it reaches either a true or a false. If it does not reach a true or a false, it has to convert it to a true or a false. As a general rule, 0 evaluates to false, and everything else evaluates to true.
So if(-1) is true, as is if(234) and so on.
The expression 10 == true already evaluates to false, so no further conversion is needed. if(10) is neither true or false, so the compiler has to convert it, using our rule above, and it becomes true.
I am creating a Sudoku solver in C++ while implementing Lua scripting for the actual solving of the puzzle. I have created the following Lua code, but get a
PANIC: unprotected error in call to Lua API (attempt to call a nil value)
error whenever my C++ code reaches the first instance of lua_call.
When compiling the code in SciTE, I get the following error:
lua: SudokuSolver.lua:99: 'end' expected (to close 'for' at line 61)
near ''
Adding three 'end's to the end of the function that has the for loop at line 61 clears that error, but causes errors in the C++ program. Can someone please look at my Lua and see if there's any syntax errors or other issues which may be causing this? Thank you
CODE
-- Table Declaration
SudokuGrid = {}
function RecieveGrid ( _Pos, _Value )
-- Recives the cell value at _Pos position from C++
SudokuGrid[_Pos] = _Value
end
function SolveSudoku ( _Pos )
-- Recursive function which solves the sudoku puzzle
local iNewValue = 1
-- If Position is 82+, all cells are solved
if( _Pos >= 82 ) then
return true
end
-- If Position already has a value
if( SudokuGrid[_Pos] ~= 0) then
return SolveSudoku( _Pos + 1 )
else
while(true) do
SudokuGrid[_Pos] = iNewValue
iNewValue = iNewValue + 1
-- If the new value of the cell is higher than 9 its not valid
if( SudokuGrid[_Pos] > 9 ) then
--Reset value
SudokuGrid[_Pos] = 0
return false
end
if( IsValid( _Pos ) and SolveSudoku( _Pos + 1 ) ) then
return true
end
end
end
end
function IsValid ( _Pos )
-- Calculate Column and Row in Grid
x = _Pos % 9
if( x == 0 ) then
x = 9
end
y = math.ceil(_Pos / 9)
-- Check Rows
for i=1, 9 do
CheckVal = ((y - 1) * 9) + i
if( CheckVal == _Pos ) then
-- Do nothing
else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal]and SudokuGrid[_Pos] ~= 0 ) then
return false
else
-- Do nothing
end
end
-- Check Columns
for i=1, 9 do
CheckVal = ((i - 1) * 9) + x
if( CheckVal == _Pos ) then
-- Do nothing
else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal] and SudokuGrid[_Pos] ~= 0 ) then
return false
else
-- Do nothing
end
end
-- Check 3X3 Grid
SquareCol = math.ceil(x/3)
SquareRow = math.ceil(y/3)
StartVal = (SquareCol - 1) * 27 + (SquareRow * 3) -2
for j=0, 2 do
for i=0, 2 do
CheckVal = StartVal + i
if( CheckVal == _Pos ) then
-- Do nothing
else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal] and SudokuGrid[_Pos] ~= 0 ) then
return false
else
-- Do nothing
end
end
StartVal = StartVal + 9
end
return true
end
function SendGrid ( _Pos )
-- Sends the value at _Pos to C++
return SudokuGrid[_Pos]
end
The syntax error is in all lines containing else if:
else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal]and SudokuGrid[_Pos] ~= 0 ) then
In Lua, use elseif instead. Using else if would need more closing end.
elseif SudokuGrid[_Pos] == SudokuGrid[CheckVal] and SudokuGrid[_Pos] ~= 0 then
I am using this statement
if ((pm && pn) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
So now it is acting like this:
0 0 = 1
0 1 = 0
1 0 = 1
1 1 = 1
but I need it to work like this:
0 0 = 1
0 1 = 0
1 0 = 0
1 1 = 1
can you tell me where am I making mistake?
What you want is simply:
if (pm == pn)
You are checking if pm is true twice. You also need to check if both are the same, not whether they are both true. So,
if ((pm == pn)
^^ ^^
pm && pm
should be
pm && pn
^
The whole expression can be simplified to
pm == pn
if the variables already have bool type.
Why not try xor?
if (!(pm ^ pn)) { /*...*/ }
Or simply equal?
if (pm == pn) { /*...*/ }
if ((pm && pm) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
Because you made a typo. You meant pm && pn.
Instead just write if (pm == pn), which is equivalent along as the only semantic values are indeed true and false for both variables.
Plus, consider making your variable names clearer and more distinct.
Note that operator precedence has nothing to do with this.
Since the question's title asks about precedence, note that || has lower precedence than &&. So the two sets of inner parentheses are redundant, and the original expression is just a longer way of saying
if (pm && pm || pm == false && pn == false)
Now, fixing the obvious typo:
if (pm && pn || pm == false && pn == false)
Removing the unneeded explicit comparisons:
if (pm && pn || !pm && !pn)
And, finally, a less obvious transformation, which others have suggested:
if (pm == pn)
Just a basic Casaer Cipher. I've tested all of the sub functions, just encryptChar() does not particularly work. I get an infinite loop. It's supposed to be recursive. Here's the all code:
fun replace (str : string, index : int, newChar : char) : string = String.substring(str,0,index) ^ String.str(newChar) ^ String.substring(str,index+1,(size str) - index - 1;
fun encryptChar (msgStr : string, shiftAmnt : int, index : int) : string =
let val asciiCode = 0
in
if (not (String.sub(msgStr, index) = #" ")) then
(
asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;
if (asciiCode < ord(#"A")) then asciiCode = asciiCode + 26
else if (asciiCode > ord(#"Z")) then asciiCode = asciiCode - 26
else asciiCode = asciiCode;
msgStr = replace(msgStr, index, chr(asciiCode))
)
else asciiCode = asciiCode;
index = index + 1;
if (index < (size msgStr - 1)) then encryptChar(msgStr, shiftAmnt, index)
else msgStr
end
;
fun encrypt(msgStr : string, shiftAmnt : int) : string = encryptChar (String.map Char.toUpper msgStr, shiftAmnt mod 26, 0);
The problem here is that you're misusing =. Outside of a variable definition, = is simply a boolean function which checks its arguments for equality. So if you do for example asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;, it will simply return false (because asciiCode is not equal to ord( String.sub(msgStr, index) ) + shiftAmnt) and then throw that result away (because you have additional expressions after the ;). It will not reassign asciiCode.
Variables in SML are immutable. If you want to emulate mutable variables you can use refs and the := operator. However I would not recommend that approach as it is generally not good functional style and not necessary in this case. The preferable approach would be to rewrite the code in a way that each variable is only assigned once.
This is very basic indeed, and it's surprising that you ran into it in such a complicated situation.
Did you port this from some other language?
You need to forget everything you know about programming using assignments.
let val x = y in something
means more or less "within 'something', replace the identifier 'x' with the value of 'y'".
There is no way for you to change the value of x.
Do the substitution (this is not the actual evaluation order or anything, but it should give you an idea of what's going on):
encryptChar("THIS", amount, 0)
=>
let val asciiCode = 0
in
if (not (String.sub("THIS", 0) = #" ")) then
(
asciiCode = ord( String.sub("THIS", 0) ) + amount;
if (asciiCode < ord(#"A")) then asciiCode = asciiCode + 26
else if (asciiCode > ord(#"Z")) then asciiCode = asciiCode - 26
else asciiCode = asciiCode;
"THIS" = replace("THIS", 0, chr(asciiCode))
)
else asciiCode = asciiCode;
0 = 0 + 1;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else str
end ;
=>
if (not (String.sub("THIS", 0) = #" ")) then
(
0 = ord( String.sub("THIS", 0) ) + amount;
if (0 < ord(#"A")) then 0 = 0 + 26
else if (0 > ord(#"Z")) then 0 = 0 - 26
else 0 = 0;
"THIS" = replace("THIS", 0, chr(0))
)
else 0 = 0;
0 = 0 + 1;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else str
=>
if (not (String.sub("THIS", 0) = #" ")) then
(
0 = ord( String.sub("THIS", 0) ) + amount;
if true then false
else if false then false
else true;
false
)
else true;
false;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else "this"
->
if (not false) then
(
false;
false;
false
)
else true;
false;
if true then encryptChar("THIS", amount, 0)
else "THIS"
=>
(
false;
false;
false
)
false;
encryptChar("THIS", amount, 0)
=>
encryptChar("THIS", amount, 0)
Which is where your infinite loop came from.
You would do well to get hold of an introductory text about ML programming.