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.
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.
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
Why does A resolve true, but B does not?
Bosses = {
'A' : 5,
'B' : [5,6]
}
for key, value in Bosses.iteritems():
if value == 5:
print "Yes for " + key
else:
print "No for " + key
You could add an additional check like this
if (isinstance(value, list) and 5 in value) or value == 5:
print "Yes for " + key
else:
print "No for " + key
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 6 years ago.
Improve this question
given a string in this format:
07:05:45PM
I am to convert it to military time.
My idea is to check element 8 of the string for whether it is a 'P' or an 'A' and modify the string accordingly however this expression:
if (time[8] == 'P' );
always evaluates as true whether time[8] is an 'A' or a 'P' or even a '7'
why?
Because you have an extra semicolon, right after the if statement.
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 8 years ago.
Improve this question
I'm trying to create a simple program to take serial numbers as an input and then searche them to find out what year the product was made in.
I am having some trouble getting a std::string to hold both numbers and letters.
std::string serial;
serial = b1234;
For example, if I run this through my compiler, I get the error message 'invalid digit in decimal constant'.
Is there a simple way to hold letters and numbers together in a string?
Basic C++ syntax says that string literals are delimited by " quotation marks.
const std::string serial = "b1234";
No. A a std::string is a string of characters, so you can't save integers in that string.
You're trying to write "b1234", I guess. Have you read any tutorial on C++ strings?