what is the specific usecase for !! in C++ [duplicate] - c++

This question already has answers here:
Double Negation in C++
(14 answers)
Closed 2 years ago.
Came across using !! in C++ during condition check
if ( !! (flag != 0 )){..
}
This could be directly used like
if( flag != 0 ){..
}
Is there any specific corner use case in C/C++ or is it just a form of coding style ?

In this case, it's superfluous, but in general this style is used to convert the actual expression value to
an integer type in C.
a boolean type in C++.

Related

Is there a C or C++ equivalent to 'pass' in python? [duplicate]

This question already has answers here:
Is there an equivalent of Python's `pass` in c++ std11?
(7 answers)
Closed 2 years ago.
Is there a C or C++ equivalent to 'pass' in python? Also the same with break. For example:
while True:
if x == 1:
break
else:
pass
But in C/C++ instead?
Is there a C or C++ equivalent to 'pass' in python?
Yes. There are actually two equivalents. One is the null statement:
;
Another is the empty block statement:
{}
These are in most cases inter-changeable except the null statement cannot be used in all cases where the empty block statement can. For example, the function body must be a block statement.
In the example case, you can omit the else-statement entirely just like you can in Python.

How to use strings with switch [duplicate]

This question already has answers here:
Why can't the switch statement be applied to strings?
(22 answers)
Evaluate a string with a switch in C++ [duplicate]
(7 answers)
C/C++ switch case with string [duplicate]
(10 answers)
C/C++: switch for non-integers
(17 answers)
Closed 2 years ago.
I am brand new to programming. I have only learned up to functions in c++ so far, so please answer with in the scope of my knowledge.
I am working of a bank account program and I want to use a switch to get the user to input whether they want to deposit or withdraw.
I know I can use an int or even a char and do some like “D” for deposit but that’s not what I want to do.
I want to able to use the whole string “deposit” with the switch statement.
Thank you!
The switch statement in c++ only supports integer types or values that can be evaluated to an integer. At best you could write a function to evaluate your strings to an integer as mentioned here
ref: Evaluate a string with a switch in C++

Comparing bool with 1 in C++ [duplicate]

This question already has answers here:
Can I assume (bool)true == (int)1 for any C++ compiler?
(5 answers)
Closed 7 years ago.
Is it correct to compare bool to 1?
In a legacy code I find often:
if (xyz.isCounterActive() == 1)
where sCounterActive() returns bool.
Obviously, if ( xyz.isCounterActive() ) is sufficient, but If I change this, I don't know which side-effects it may cause. Software is big, buggy but the customer insists, that it is working.
Compiler is VS2008
In this case result of xyz.isCounterActive() will be implicitly converted to int. There're many rules of implicit conversion, which can be found here, for example.
Probably signature of isCounterActive changed since it was introduced, and the one, who changed it, forgot to modify all isCounterActive calls.

A multiple equality/inequalityconditions checking in if statements [duplicate]

This question already has answers here:
How can I check whether multiple variables are equal to the same value?
(3 answers)
Closed 7 years ago.
I am a newbie to programming in C++ and I have a question regarding if conditions. We are currently learning C++ at school(Using TC, i know it's an old compiler,but yeah). I am currently making a tic-tac-toe program, an undefeatable one. Now, this is my problem.
I want to check the equality of 3 variables, and run the if body only if the 3 variables are not equal to another variable.
Why is this set of code not working?
if(a==b==c!=d)
{
}
Adding parentheses doesn't help, I'm probably doing it wrong.(Please excuse my ignorance)
if((a==b==c)!=d)
{
}
Thanks in advance!
-CaptainAwesome
You have to write each condition individually and combine them using && (logical and):
if(a==b && b==c && c!=d)
{
// ...
}
Because you made this up. You can't do boolean comparisons like this.
Stick to two operands at a time and use && and || to combine results.
I'm not entirely clear on your requirements but start with something like this:
if (a == b && b == c && c != d)

boolean operator in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does 'unsigned temp:3' mean?
I saw some c++ code today that used single colons.
bool variable_name : 1;
what is the difference between this and
bool variable_name = true;
The ": 1" means it is a bit field with 1 bit, or at least that's what it means in C. It probably was put there to save some memory, allowing multiple bools to be stored in the same byte. The downside is that you probably can't make a pointer to that bool.