#include <iostream.h>
int main() {
int choice;
cin>>choice;
if (1<=choice<=3) cout<<"good";
else cout<<"bad";
return 0;
}
How is the bool expression is evaluated? is this expression equal to
if ((1<=choice)||(choice<=3))
if (1<=choice<=3) cout<<"good";
Is like writing:
if ((1<=choice)<=3) cout<<"good";
Which is always satisfied because 1<=choice returns 0 or 1 (0 is false and 1 is true), which is always <=3.
What you (probably) want to do is:
if(1 <= choice && choice <=3)
(1<=choice<=3)
is not equivalent to:
(1 <= choice || choice <= 3)
but rather:
((1 <= choice) <= 3)
Which is always going to be true, since int(1 <= choice) is equal to 0 or 1
I assume that what you want is:
(1 <= choice && choice <= 3)
You need this.
if (1<=choice && choice <=3) cout<<"good";
Related
I have tried writing a loop that would refrain the user to enter a wrong kind of data (actually a boolean) into the program by using the || operator.
int Entrer()
{
int A;
do
{
cout<<"Entrez 0 ou 1."<<endl;
cin >> A;
}
while (A != (1 || 0));
return A;
}
Can somebody tell me why the program only accepts 1 and no 0 ?
do { ... } while (A != (1 || 0));
It should be while (A != 1 && A != 0);
Otherwise, A != (1 || 0) stands for A != 1 since (1 || 0) is evaluated before !=.
If you want to accept 1 and 0, you need to write the conditional as while(A != 1 && A != 0);. As your conditional written, it will evaluate the (1 || 0) first, and, as 1 is true and 0 is false, will evaluate to A != 1.
I've got a double[9] and want to check if it contains the values (1,0,0,0,1,0,0,0,1). Is there a cleaner way than this?
if (ornt1[0] == 1 && ornt1[1] == 0 && ornt1[2] == 0
&& ornt1[3] == 0 && ornt1[4] == 1 && ornt1[5] == 0
&& ornt1[6] == 0 && ornt1[7] == 0 && ornt1[8] == 1 )
I'm using C++.
It is not a good idea to compare double values strictly. I would recommend you create a constant array to compare against and then use a cycle and also use a tolerance(e.g. 1e-9):
bool doublesEqual(double a, double b) {
return fabs(a - b) < 1e-9;
}
const double expected[9] = {1,0,0,0,1, 0, 0, 0, 1};
bool equal = true;
for (int i = 0; i< 9; ++i) {
if (!doublesEqual(expected[i], ornt1[i])) {
equal = false;
break;
}
}
if (equal) { // do smth
EDIT: as suggested by John Zwinck I have edited the code to be able to handle the case when the array we compare contains only NAN. I have edited his suggestion a bit to make the code more readable. Please refer to his comment below for clarification why this is needed.
Hi I have these two separate if statements, when put like so ;
if (powerlevel <= 0) // <--- ends up having no effect
if (src.health <= 0)
the_thing_to_do();
How do I combine these two if statements into one? is it possible? If so how?
If you want both statements to be true use logical AND
if(powerlevel <= 0 && src.health <= 0)
If you want either of the statements to be true use logical OR
if(powerlevel <= 0 || src.health <= 0)
Both of the above operators are logical operators
Use operator&& if you want both of them to be met (logical AND)
if(powerlevel <= 0 && src.health <= 0) { .. }
or operator|| if you want just one to be met (logical OR)
if(powerlevel <= 0 || src.health <= 0) { .. }
It depends if you want both to evaluate to true...
if ((powerlevel <= 0) && (src.health <= 0)) {
// do stuff
}
... or at least one ...
if ((powerlevel <= 0) || (src.health <= 0)) {
// do stuff
}
The difference being logical AND (&&) or logical OR (||)
Just an aternative if it is meaningful(sometimes).
Both true:
if (!(src.health > 0 || powerlevel > 0)) {}
at least one is true:
if (!(src.health > 0 && powerlevel > 0)) {}
Or if you don't want to use && you can use a Ternary Operator
#include <iostream>
int main (int argc, char* argv[])
{
struct
{
int health;
} src;
int powerlevel = 1;
src.health = 1;
bool result((powerlevel <= 0) ? ((src.health <=0) ? true : false) : false);
std::cout << "Result: " << result << std::endl;
}
The result prints out 'c' 3 times, anyone know why it always meets the first condition?
#include <iostream>
using namespace std;
char x(char y)
{
if (y == 'a' || 'b')
{
return 'c';
}
else if (y == 'c' || 'd')
{
return 'e';
}
else
{
return 'g';
}
}
int main()
{
cout << x('a') << endl;
cout << x('c') << endl;
cout << x('p') << endl;
return 0;
}
You need something of the form
if (y == 'a' || y == 'b')
This is because in this expression
(y == 'a' || 'b')
you are evaluating an OR of y == 'a' and 'b', and since 'b' evaluates to true by virtue of being non-zero, the whole expression evaluates to true.
(y == 'a' || true)
This line:
if (y == 'a' || 'b')
is equivalent to:
if ((y == 'a') || ('b'))
That's because the == operator has higher precedence than the || operator.
Since 'b' is non-zero, it always evaluates as true, and so (y == 'a' || 'b') always evaluates as true.
You need to write this:
if (y == 'a' || y == 'b')
Of course, even if the precedence was the other way around,
if (y == ('a' || 'b'))
would not have been what you intended either!
Please check the operator precedence (priority) here: http://en.cppreference.com/w/cpp/language/operator_precedence
In your case the condition expression is:
(y == 'a' || 'b')
So the “y == 'a'” part is evaluated first which may be true/false depending on the value of "y". Let's call the value of "y=='a'" as "t". And then the expression is evaluated as " t || 'b'" in this case 'b' is actually the ASCII code value of character 'b' (98) which is surely larger than 0, so the result of the boolean expression is always true.
To dismiss any ambiguity caused by operator precedence, I think it's a good habit to use brackets to explicitly express your priority in the evaluation. In your case, as suggested by earlier post, it should be:
if ((y=='a') || (y=='b'))
I'm trying to write a c++ program to read a line of inputed code (eg The Hat is flat) and get the program to output the number of capital letters the code has. (in this example it would have to output 2). I have written a piece of code using cin.get() but my code is not entering the while loop. Please help me. and please change 'my code' only.
#include <iostream>
using namespace std;
int main ()
{
char y = 0;
int capitals = 0;
int flag = 0;
cout << "Enter your line of words followed by a fullstop: ";
while (!flag == -1)
{
cin.get(y);
if ((y >= 65) && (y <= 90))
{
capitals = capitals + 1;
}
if (y == 46)
{
flag = -1;
}
}
cout << "Number of capitals equal is this line is: " << capitals << endl;
return 0;
}
flag is initialised to 0, !flag is therefore 1, and your while loop is never entered.
You could fix this with:
while (!(flag == -1))
or
while (flag != -1)
Instead of !flag == -1 use flag != -1.
! is a unary logical NOT operator, and !0 is 1, not -1.
Have you tried this?
while (!(flag == -1))
or
while (flag != -1)
I think you need while (!(flag == -1)) ... but it would be better to write while (flag != -1). The ! operator has higher precedence so it is evaluated before ==.
Also you should try to use isupper() to test for uppercase -- don't reinvent the wheel.
I think the error is operator precedence. !flag == -1 is equivalent to (!flag) == -1. Try flag != -1.
If you'll forgive a probably-too-complete answer to a probable homework question:
while (cin >> y && y != '.')
capitals += (bool)isupper(y);