Two && in a while condition? - c++

I was trying to help a friend with a problem, he asked me how can he make the GCD of 3 numbers using the Nicomachus method. Everything went great until I tried to enter this condition:
while (a!=b && b!=c && a!=c)
But the problem is it would only execute once. I changed it into this:
while (a!=b && b!=c)
I know it results into the same thing but I was just wondering why the first one can't work ? I can't add 2 && in the same condition ?

You certainly can have 2 && in the same condition - you can have as many as you want. The fact that the loop stops depends on something else.
By the way, if you had a=10, b=20, C=10, the first condition (while (a!=b && b!=c && a!=c)) would stop (because a != c would be false), but the second one (while (a!=b && b!=c)) would continue. Probably this is what happened.
Transforming 3 conditions into just 2 can be done in the opposite case, that is, if you want to make sure they are all equal: a ==b && b == c automatically implies that a == c (see Transitive relation of equality), so adding this 3rd condition or not doesn't make any difference. But the same is not true for inequality, as I have shown.

Related

Velocity Template Language: Connect two conditions in one #elseif() statement

In my example the parameters are:
$p2.length
which has the value "0"
and the boolean
$suspectYesNo which is set to "false".
I would expect, that in my code the second #elseif will run due to my set parameters, but however it shows up that "Code 4 runs"
I guess, that my mistake is the connection of both conditions using "&&", but I have no clue how to correctly define the line with two conditions.
That brings me to the question: How to correctly combine the two conditions ($p2.length()==0) AND ($suspectYesNo.value.contains("true")) in one #elseif().
Or did I define the boolean wrong?
#if($p2.length()>0)
Code 1 runs
#elseif($p2.length()==0 && $suspectYesNo.value.contains("true"))
Code 2 runs
#elseif($p2.length()==0 && $suspectYesNo.value.contains("false"))
Code 3 runs
#else
Code 4 runs
#end
Thanks to user7294900 for providing me a reference with which I could solve the problem.
It did not read the boolean value as a string, which could be fixed by adding "toString()" method.
With this code here it works as desired:
#if($p2.length > 0)
Code 1 runs
#elseif($p2.length == 0 && $suspectYesNo.value.toString().contains("true"))
Code 2 runs
#elseif($p2.length == 0 && $suspectYesNo.value.toString().contains("false"))
Code 3 runs
#else
Code 4 runs
#end

Bug in a fixed If - Else statement

this is my first post in this site.
As the title says, I encountered a bug in an If - else if - else block of codes.
What the problem is that, I am pretty confident that I have covered all 9 possible outcomes that there should be.
It is quite hard to explain more, so if you guys could take a look at my .cpp file and run it ( hopefully it runs because I used c++ on a mac), you may find an easier understanding of my problem.
source code cpp file
If you browse through my action();, in the else statement, I purposely displayed that you encountered a bug so just in case there were bugs, I'd be informed.
What I am trying to do is like this:
userinput | randomAImove | outcome
A 1 statement
A 2 statement
A 3 statement
D 1 statement
D 2 statement
D 3 statement
W 1 statement
W 2 statement
W 3 statement
else 1||2||3 statement
There are corresponding statements to each conditions met.
'A', 'W', 'D' are user input. I used a toupper command to force the compiler to just check on 'A' 'W' 'D'. However, the main problem I find is that, even if the toupper works(which I confirmed), the program displays the correct statement often but still somehow manages to bug even if the user(I tried it) input from A, W, D (not-case sensitive cuz of toupper).
Please just try it and after a few tries you may encounter it also. I wrote "you encountered a bug" if you ever do encounter it.
Please help me because I can't really see any other way to find the mistake. Maybe its in my plain sight but I can't seem to notice which one is it.
Thank you!
Change the AImove function to
void AImove()
{
turn = rand () % 3 + 1;
}
(add + 1), or you may get 0 in turn and it will lead you "encounter a bug".

If statement compatibility issue in Swift (it worked before)

i reopened a project i stop working on for some days and without edditing any line, when i tried to run it, it gave me two errors i never saw before and i persoanlly think they make no sense. Please help me, the code is like this:
if normals == true && numberOfTaps > highScoreN.integerForKey("highscoren") || normals == 1 && highScoreN.integerForKey("highscoren") == 0
It gives me this error: "Cannot invoke '==' with an argument list of type '($T16, $T31)'"
and in another line, which is practically the same:
if pros == true && numberOfTaps > highScoreP.integerForKey("highscorep") || pros == 1 && highScoreP.integerForKey("highscorep") == 0
It gives me this error: "Cannot invoke '==' with an argument list of type '($T16, $T31)'"
I repeat, it had worked properly some days before, i dont know why it isn't..
Thank you so much!
PSD:// highScoreP and highScoreN are NSUserDefault type
The error is on the == in both lines, so that narrows it down. The only place you have == is comparing the normals variable to true and 1. Swift is very type safe, so your normals variable cannot be both true (a boolean) and 1 (an Int). Check the type of your normals variable and proceed form there.

"Invalid expression, assumed zero" error on if-statement

I'm getting the following error whenever I compile my code: "Error 029: Invalid expression, assumed zero"
The error is thrown on the following line:
if ((PlayerInfo[playerid][ADMINLevel])) || (IsPlayerAdmin(playerid))
I want the if-statement to check if "ADMINLevel" is above zero or if the player is logged in as an RCON admin.
You're constructing your if-statement wrong. The correct way to do it is
if(PlayerInfo[playerid][ADMINLevel] > 0 || IsPlayerAdmin(playerid))
{
/* Put your desired script here */
}
Your code was nearly correct (although it did have some unnecessary brackets), you just need to actually add a comparison to the ADMINLevel check. An if-statement should be like a question ("is admin level more than 0", rather than just "is admin level"). You can find more information about if-statements in Pawn here, and I think it will be useful for you to read.
PlayerInfo[..][..] does not return a boolean. Add > 0 to fix it

Small c++ game using graphics.h , calling multiple functions at once ? (multi threading?)

I'm writing a small ball game. I have a function called gravity and I also have a while loop that checks whether the user wants to move the ball using wsad keys.
Do I need to multi thread this or is there another way out? I cut out some of the irrelevant code for setting up the program. Here is just the stuff that matters:
while(1) {
enableGravity();
char ch = getch();// i know getch() is not going to cut it
//maybe 2 different f() multi threaded , for gravity and position,
f(ch == 'w' || ch == 'W')
updateObjPosition('U');
else if(ch == 's' || ch == 'S')
updateObjPosition('D');
else if(ch == 'a' || ch == 'A')
updateObjPosition('L');
else if(ch == 'd' || ch == 'D')
updateObjPosition('R');
}
I have these functions in main. I need the program to enable gravity and also be able to accept input to move the ball through updateObjPosition() simultaneously.
You could use multiple threads. A more obvious possibility would be a non-blocking keyboard read.
If you're doing this on Windows, you probably have a _kbhit in your standard library that will tell you if a key on the keyboard has been pressed. If you're using curses, you can use nodelay to tell getch to return immediately, whether a key has been pressed or not. Other systems may do things in different ways still, but you get the general idea...
You don't need to multi-thread. You just need an API that returns TRUE if a specified key is currently pressed down. That will likely be platform dependent. What platform are you building/running on?
For example on Win32: GetAsyncKeyState
As you have mentioned you are using old Borland compiler on TC 3.1 in the old conio.h there was a function
kbhit();
the function was a one shot solution for getting if any key is pressed on the keyboard
so here is your loop to get key input
int getKey(){
if(kbhit){
return getch();
}
return -1;
}
By they way you should upgrade to VC 08 Atleast!
For your simple project where the logic loop i guess will be quite small this will handle all the pain for you but if you write a longer application with hard logic then you will have to use better and Asynchronous methods for IO and those methods basically involve working on 2 threads 1 getting Input and the other doing logic operations ..