error C2106: '=' : left operand must be l-value with `!=` C++ - c++

I have absolutely no idea why my compiler is throwing this error:
"Error 1 error C2106: '=' : left operand must be l-value" with this line of code:
while ((input != 1 && input != 2 && input != 3 && choice = 1) ||
(input != 1 && input != 2 && input != 3 && input != 4 && input != 5 && input != 6 && choice = 1) ||
std::cin.fail()){}
The error appears to want input, which is a short, to be modifiable, but != is not a modifier, it's a checker. What is going on?

Operator precedence dictates that your first sub-condition
input != 1 && input != 2 && input != 3 && choice = 1
is parsed as
(input != 1 && input != 2 && input != 3 && choice) = 1
The left-hand side of = is not an lvalue, just like your compiler told you.
What were you trying to say by that strange combination of operators?

Your left side of (=) must be l-value, which means it must have adress in memory

Related

Why does this while loop keep looping even when its false? In C++

This is the code in question:
int integer;
while (integer != 0 || integer != 1) {
cout << "Choose an integer: \n0\n1\n";
cin >> integer;
}
When I type 1 it continues looping even though the statement is false.
I have had this problem before or similar but it got fixed in a weird way that seems to not be working right now.
The other code that was having problems was this one:
while(chosen != 1 || chosen != 2 || chosen != 3)
{
cin >> chosen;
}
I got it fixed by doing this:
while(chosen < 1 || chosen > 3)
Does annyone know whats happening here? Ty in advance!
let me put you out of your misery
while(chosen != 1 && chosen != 2 && chosen != 3)
{
cin >> chosen;
}
This is a common issue, people translate the human idea in their heads into code: "if its not 1 or 2 or 3 then do xx". But that doesnt work.
(chosen != 1 || chosen != 2 || chosen != 3)
will always be true.
If chosen is say 0 then chosen != 1 is true. So the overall condition is true.
If chosen is 1 (which should be the end of your loop) then chosen !=1 is false, BUT chosen != 2 is true so the overall condition is still true (its true if one of the clauses is true, this is what 'or' / '||' means).
In fact there is no value for chosen which will cause the overall condition to be false. Chosen is always going to not equal one of 1 or 2 or 3.
Your problem came from the looseness of human thought, in conversation we would get what you mean, but not computers. What you wanted was "if its not 1 and its not 2 and its not 3 do xx". Ie
while(chosen != 1 && chosen != 2 && chosen != 3)

C memcmp third parameter type

Third argument in memcmp should be of type sizeof(). I read this code with the following
memcmp(st.magic, "outpt_01",sizeof(st.magic)) == 0 && st.version == 0 )
where st is a struct.
What is the type of the third parameter in the call above? what is it doing ?
What is the type of the third parameter in the call above?
size_t Number of bytes to compare.
what is it doing ?
if (sizeof(st.magic) == 0 && st.version == 0)
memcmp(st.magic, "outpt_01", 1);
else
memcmp(st.magic, "outpt_01", 0);
As pointed out by #JoachimPileborg, there is a typo in:
memcmp(st.magic, "outpt_01",sizeof(st.magic)) == 0 && st.version == 0 )
------------------------------------------------------------------------^ extra closing parenthesis
or it is part of a condition like (makes more sense):
if (memcmp(st.magic, "outpt_01",sizeof(st.magic)) == 0 && st.version == 0) {

C++ infinite loop

I am attempting to write a loop that will repeat until the user enters one of the correct choices (either 1 or 0). For some reason when I have the loop written as below it creates an infinite loop.
I am intending for the loop to only execute while control is not 0 OR not 1, but for some reason it will always execute and becomes an infinite loop.
cout<<"Please enter 1 for another customer or 0 to quit : ";
cin>>control;
while ((control != 0 )|| (control != 1))
{
cout<<"Invalid Entry! Please enter a 1 to enter another customer or 0 to quit: ";
cin>>control;
}
I changed it to be while control less than 0 OR greater than 1, which works but I am still confused as to why the other loop is not working.
You have to use && operator.
while ((control != 0 ) && (control != 1))
(control != 0) || (control != 1)
is equivalent to,
!(control == 0 && control == 1)
but,
(control == 0 && control == 1)
is always false (there is no such number).
Therefore, the whole expression will always get true value.
The only way to break out
while ((control != 0 )|| (control != 1))
is
!(control != 0) && !(control != 1)
which is equivalent to
control == 0 && control == 1
which is impossible for all integers.

If statement not seeming to check other && in statement

I have an if statement that looks as follows:
int count=0;
string Check;
if ((count==4 && Check!="-s")||(count==4 && Check!="-S"))
If count equals 4 and Check equals "-s" or "-S" it still enters this if statement because of the count == 4. It totally seems to ignore the second part. Is there something I'm doing wrong?
It's always going to be the case that either Check!="-s" or Check!="-S". Hence, your if statement is equivalent to if (count==4).
Well, if Check is "-S", then it will not even check the second pair of conditions, because you check with ||. The same holds true for the opposite case. If one is false, the other is true. Replace that with a &&.
int count = 4;
string Check = "-S";
if( (count == 4 && // count is 4, alright.
Check != "-s") || // Check is "-S", alright I'm done thanks to || (OR)
(count == 4 &&
Check != "-S") )
{
// ...
}
int count = 4;
string Check = "-s";
if( (count == 4 && // count is 4, alright.
Check != "-s") || // Check is "-S", time to check the other condition pair...
(count == 4 && // count is 4, alright.
Check != "-S") ) // Check is "-s", which is different from "-S", perfect.
{
// ...
}
Now the corrected version:
int count = 4;
string Check = "-S";
if( (count == 4 && // count is 4, alright.
Check != "-s") && // Check is "-S", different from "-s", now on to the other condition!
(count == 4 && // count is 4, alright.
Check != "-S") ) // Check is "-S"... oh dang! No executed code for you.
{
// ...
}
If count == 4 and Check == "-s", then the expression to the right of the || is true. If count == 4 and Check == "-S", then the expression to the left of the || is true. So you have true or true which is true. Thus, your if-block is executed.
The right statement is:
if(count==4 && (Check != "-s" || Check!="-S"))
The statement that you wrote is true if you have count = 4 and Check = "-S" because then the first part of the OR is true.
Might be more clear to use:
if (count==4 && Check!="-s" && Check!="-S")
You should use !strcmp(Check, "-s") and !strcmp(Check, "-S") instead of !=.
If you use == you compare the pointers and that is no what you want. The pointers will always be different thus your second argument will always be true.
You want to enter the if body if and only if Check is != from either -s or -S and count is = 4 right?
if ( (Check!="-s" && Check!="-S") && count==4 )
should work.
or
if ( Check.tolower() !="-s" && count==4 )
should work.
(Do not remember the name of the function to lowercase a string, you have got to look it up)
Hope this help.

How do C++ operators work

Given that x = 2, y = 1, and z = 0, what will the following statement display?
printf("answer = %d\n", (x || !y && z));
It was on a quiz and I got it wrong, I don't remember my professor covering this, someone enlighten me please... I know the answer I get is 1, but why?
The expression is interpreted as x || (!y &&z)(check out the precedence of the operators ||, ! and &&.
|| is a short-circuiting operator. If the left operand is true (in case of ||) the right side operand need not be evaluated.
In your case x is true, so being a boolean expression the result would be 1.
EDIT.
The order of evaluation of && and || is guaranteed to be from left to right.
If I'm not mistaken, it will print 1. (Let's assume short circuiting is off)
(x || !y && z) or (true || !true && false) will first evaluate the ! operator giving (true || false && false)
Then the &&: (true || false)
Then || : true
Printf will interpret true in decimal as 1. So it will print answer = 1\n
Given that x = 2, y = 1, and z = 0,
what will the following statement
display?
printf("answer = %d\n", (x || !y && z));
Ok - feeling a bit guilty for the harsh quip re poor wording of the question, so I'll try to help you in a different way to the other answers... :-)
When you've a question like this, break it down into manageable chunks.
Try:
int x = 2, y = 1, z = 0;
printf("true == %d\n", 10 > 2); // prints "1"
printf("false == %d\n", 1 == 2); // prints "0"
printf("!y == %d\n", !y); // prints "0"
printf("(x || !y) == %d\n", x || !y); // "1" - SEE COMMENTS BELOW
printf("(!y || z) == %d\n", !y || z); // "0"
printf("(x || !y && z) == %d\n", x || !y && z); // "1"
In the output there, you've got everything you need to deduce what's happening:
true == 1 reveals how C/C++ convert truthful boolean expressions to the integral value 1 for printf, irrespective of the values appearing in the boolean expression
false == 0 reveals how C/C++ converts false expressions to "0"
(!y) == 0 because ! is the logical not operator, and C/C++ consider 0 to be the only integral value corresponding to false, while all others are true, so !1 == !true == false == 0
(x || !y) == 1, and you know !y is 0, so substituting known values and simplifying: (2 || 0) == 1 is equivalent to (true or false) == true... that's understandable as a logical rule
(!y || z) == 0 - substituting known values: (0 || 0) == (false or false) == false == 0
(x || !y && z) == 1: here's the crunch! From above, we know:
x || !y is 1/true, which if relevant would imply 1/true && z/0/false == 1/true <- this clearly doesn't make any sense, so it must not be the way C/C++ are calculating the answer!
(!y && z) is false, which if relevant would imply x/2/true || false == 1/true <- this is true, so it must be the implicit order.
In this way, we've derived the operator precedence - the order of evaluation of the || and && operators, from the results that the compiler is displaying, and seen that if and only if && is valuated before || then we can make some sense of the results.
answer = 1
or maybe:
answer = -27
2 || !1 && 0
2 || 0 && 0
2 || 0
true
true = non-zero value
printf("answer = %d",*true*); -> who knows
Most compilers will output answer = 1. I wouldn't confidently state that all compilers will do that though, but I am confident all compilers would return non-zero.
I'm not going to give you the outright answer because you could just compile it and run it, but the question is just testing to see if you know operator precedence.