Error in while((Status.health !0) && (Wolves.health !0) ) expected expression ")" - c++

Hey i have an error in this:
while((Status.health !0) && (Wolves.health !0) )
Can anyone see what is wrong with this ?

It's syntactically incorrect.
Assuming you want to verify that the variables are not equal to zero:
while((Status.health != 0) && (Wolves.health != 0) )

I guess you mean
while((Status.health != 0) && (Wolves.health != 0) )

Related

Use of Logical Operator in Loop Condition

In the below given code, why the || logical doesn't work, instead the loop terminates specifically when && is used ?
int main() {
char select {};
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while (select != 'q' && select != 'Q'); // <--- why || (or) doesn't work here ??
return 0;
}
This loop will go on while select is not q and it's not Q:
while (select != 'q' && select != 'Q');
This loop will go on while select is not q or it's not Q.
while (select != 'q' || select != 'Q');
Since one of them must be true, it'll go on forever.
Examples:
The user inputs q
select != 'q' evaluates to false
select != 'Q' evaluates to true
false || true evaluates to true
The user inputs Q
select != 'q' evaluates to true
select != 'Q' evaluates to false
true || false evaluates to true
You want to terminate the loop when select is equal either to 'q' or 'Q'.
The opposite condition can be written like
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( not ( select == 'q' || select == 'Q' ) );
If to open the parentheses then you will get
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( not( select == 'q' ) && not ( select == 'Q' ) );
that in turn is equivalent to
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( select != 'q' && select != 'Q' );
Consider the following diagrams:
The full ellipse are all characters. The white dots is q and Q respectively. The black filled area depicts characters that will make the expression true. First line is select != 'q' && select != 'Q', second line is select != 'q' || select != 'Q'.
&& means both conditions must be true. The resulting black area is the overlap of the two areas on the left.
|| means either of the conditions must be true. The resulting black area is the sum of the two areas on the left.

C++ stuck in while loop [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
Hello I am doing a very simple while loop in C++ and I can not figure out why I am stuck in it even when the proper input is give.
string itemType = "";
while(!(itemType == "b") || !(itemType == "m") || !(itemType == "d") || !(itemType == "t") || !(itemType == "c")){
cout<<"Enter the item type-b,m,d,t,c:"<<endl;
cin>>itemType;
cout<<itemType<<endl;
}
cout<<itemType;
if someone can point out what I am over looking I'd very much appreciate it. It is suppossed to exit when b,m,d,t or c is entered.
Your problem is in your logic. If you look at your conditions for your while loop, the loop will repeat if the item type is not "b" or not "m" or not "d" etc. That means if your item type is "b", it is obviously not "m", so it will repeat. You want to use && instead of ||.
As other answers and comments wrote correctly your logic is wrong. Using find() would simplify your task:
std::string validCharacters( "bmdtc" );
while ( std::string::npos == validCharacters.find( itemType ) )
{
...
}
This solution is more general and easier to read. See also documentation of std::string::find
The boolean expression to exit the loop is flawed. The way it is, in order to exit the loop the itemType would have to be all those letters at the same time. Try to instead || the letters first, and then negate it:
while(!(itemType == "b" || itemType == "m" || itemType == "d" || itemType == "t" || itemType == "c")
try this
string itemType = "";
while(!(itemType == "b" || itemType == "m" || itemType == "d" || itemType == "t" || itemType == "c")){
cout<<"Enter the item type-b,m,d,t,c:"<<endl;
cin>>itemType;
cout<<itemType<<endl;
}
cout<<itemType;
you condition is always true

Multiple conditions in a jade conditional

I'm a bit stumped as to why... if (data.query && data.query != '[object Object]') works
but if (data.query && data.query != ('[object Object]' || data.query != 'undefined'))
How can you have multiple conditions inside of a jade if?
Checking for typeof on the data.query for the undefined did the trick.
if (data.query && data.query != '[object Object]' || (typeof data.query !== 'undefined'))

if or statement unexpected token

I've attempting to write an in or statement, and in all honesty have never used one before. I'm trying to use the following snippet of code:
$(document).ready(function () {
if(window.location.href.indexOf("invt") > -1 || window.location.href.indexOf("shopcart") > -1)) { // if URL contains invt or shopcart
$('#carriage-promo').prop('id','newid');
}
});
But it keeps returning errors no matter what I try!
Any suggestions?
( ) are unbalanced.
if(window.location.href.indexOf("invt") > -1 ||
window.location.href.indexOf("shopcart") > -1)
I removed the last ) at the end of your if statement
if(window.location.href.indexOf("invt") > -1 || window.location.href.indexOf("shopcart") > -1))
You have one too many closing brackets )
Try:
if(window.location.href.indexOf("invt") > -1 || window.location.href.indexOf("shopcart") > -1)

Hierarchy of && and ||

Let's say I have this code (bool1, bool2 and bool3 are booleans [should be obvious)):
if (bool1 && bool2 || bool3)
When is this if-Statement true? So what if only bool3 is true and the other two booleans are false. So I want to know if it is equal to
if ((bool1 && bool2) || bool3)
or
if (bool1 && (bool2 || bool3))
I know I can simply put some more brackets in there, but my code would be shorter if not.
You need to check operator's precedence table for your language. For C++ it is:
http://en.cppreference.com/w/cpp/language/operator_precedence
13 && Logical AND
14 || Logical OR
bool1 && bool2 || bool3 is (bool1 && bool2) || bool3
It is not about if-statement it is about evaluating boolean expressions.