Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I’m trying to make a tic-tac-toe program; I'm adding an if statement to change a variable's value if the condition is met. But when the condition is met, the variable that's value should be changed is 0.
I'm doing this in c++
cout <<player1 << ", which position would you like to add your X?: " << endl;
cin >> x1;
if (x1 == (b)) {
xx1=5;
}
N.B. When it asks you which position would you like it add your X, I am typing "a".
I expected xx1's value to change to 5 but instead of 5 it was 0.
Edit: I was using the "=" operator instead of the "==" operator. I ran the code and it worked, though now, a couple minutes later, it isn't working. I don't know what I did to make this happen, because the only thing that I did was setting it up for other letters.
To check if variables are equal you should use == operator.
You can read more about comparison operators here
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I have a dynamic queue, so I want to check if the head pointer is pointing to something. So I do this:
if (mHeadPtr = NULL)
return false;
The pointer isn't empty (which is also why it does not go into the "return false" line) before this executes, but somehow it is afterwards, and I have no idea why.
If I instead check if the variable is not empty, it works without a problem.
if (mHeadPtr != NULL)
std::cout << "yay!" << std::endl;
else
return false;
How can it be that the if statement changes the variable it's only supposed to read?
For comparisions you should use == instead of =. By using a single = you don't compare the value of the variable but assign NULL to it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I wrote this "if" statement in a function with local double-type variables n4_1, n4_2 and n4_3:
if (n4_1 == 17 && n4_2 == 12 && n4_3 = 2003) { }
But Windows Studio Intellisense underlines the variables and says:
Expression must be a modifiable Ivalue
'=' : left operand must be l-value
The values of these variables are assigned through cin >> command.
I wanted to run the code inside the "if" statement ONLY if all three condition are true.
Please can you help me and explain me why is this incorrect and how can I correct it?
I'm a beginner so constructive criticism is welcome. Thanks in advance and please use simple words lol, I need to understand as clear as possible for the future.
Note that the final part of your if is an assignment. Note the single = in n4_3 = 2003.
You can only assign to n4_3 if it's a modifiable l-value: that is it can appear on the left hand side of =.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to do password handling in C++.
For example, I want the password to be 12345678, but no matter what I insert as a meaning of 'a' it always says that the password is correct, so basically it always thinks the quantity of 'a' is equal to the quantity of 'x'.
At the beginning, I tried to do it like this:
if (a = 12345678)
cout << "password is correct";
This didn't work either.
here is the picture of the code: https://imgur.com/ssl4i6F
The problem is a = 12345678 is operation that sets ato 12345678 and returns a by reference. So your if statement simply doesn't do what you want - instead of checking whether a is 12345678, it sets a to 12345678 and then checks if a isn't zero.
To fix it replace = with == for comparison.
if (a == 12345678)
cout << "password is correct";
Though, it is a bit strange that password is a number and not a string... but whatever.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have just started to learn C++ and i would like to get some help.
The user needs to type an ID number and the format has to be the following. The first character B and the other 4 any integer.
Im trying to check if the character format are right.
So far i have this:
if ((isalpha(id[0])=='B' ) && (isdigit(id.at(1))) && (isdigit(id.at(2))) ......
{
//do something
}
else
{
cout << "Wrong format" << endl;
}
but even if i type example B8745 it says wrong format.
You are comparing the result of isalpha, which is boolean, to character literal 'B'.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Ok, so, this is a bit of a weird issue to me. I have this portion of code in my program that uses an if statement to check and see if a page was found in memory, both of which are structs. However, I have an idea of what the output should be and my output is way off so I started checking through some areas to see where it could be coming from and in the process of doing that I ran into this issue. Here in this spot:
if(memory.memory[i].uid==position)
{
i=pages;
found=1; //The page was found in main memory
cout << memory.memory[i].uid << " " << position << endl;
}
The cout there is for testing purposes and is how I found this issue. The memory.memory[i].uid is always equal to 0, but the code inside the if statement runs anyway (so I get huge blocks of "0 ##" with ## increasing as it should be due to the position variable).
Is there any possible reason that would happen? It just doesn't make sense to me that the code inside the statement is being executed despite the statement being false. Both of the variables in the statement are integers.
(I'm not sure how much more of the code would be needed, it's a big program spanning 3 files so I didn't want to include all of it if I can avoid it. If more is needed though I can edit more in.)
EDIT: Wow, I'm blind. I just realized I'm modifying i before that statement so it's not the same i being used in the statement. After fixing it so the statement was before the modification of i the values are matching as they should be, sorry for the waste of time.
Re-assigning to i between the comparison and the printing can of course give the expression memory.memory[i].uid a different value. If you want to see the value that's used in the comparison, make sure you capture it before re-assigning to i.