Why is my if statement skipping the else? [closed] - c++

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 writing a small program that count the lines of code. Here is the definition of a line of code:
- any line that contains code necessary for the program to function.
- blank line is not a line of code.
- comment is not a line of code.
- if there's code and a comment right after on the same line, it counts as well.
So I have this piece of code (simple if statement):
found = lineRead.find("/*");
if(found != string::npos)
{
found = lineRead.find("*/");
if(found != string::npos)
inComment = false;
else
inComment == true;
}
return inComment;
Assume that
String lineRead = "cout<<\"helloworld!\";/*blockcomment"
Bool inComment (is true if the previous line didn't have end block comment token)
So my program reaches the first if statement because it found /* in that line, looks for */, goes to the second if statement then jumps straight to the return statement without changing inComment (which is supposed to be set to true because the text on the next line is still inside the block comment).
Anyone know why that is?

Your problem is the infamous "double equal sign."
inComment == true;
In C++, == is used for compare, NOT for assignment. I think what you want is:
inComment = true;

Double equal sign is for comparison not assignment. Probably just a typo as I assume you know that. But yea, change == to just = :-)

Related

c++ Getting non syntax use "&" to create a pointer [closed]

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 4 years ago.
Improve this question
I have been working on my code and cannot seem to get this error fixed its the c_str': non-standard syntax; use '&' to create a pointer to member im not sure what im doing wrong.
HSAMPLE sample = BASS_SampleLoad(FALSE, fi.Recv_Song().c_str, 0, 0, 1, BASS_SAMPLE_MONO);
I get the error from the fi.Recv_song.c_str variable its suppose to be a string for a file location so the the function its in can play a mp3 file from that location. ps. the recv funtion is one i made in a header file.
string &Get_Song()
{
cin >> song; //this is the header file it is in.
mpc = path + song;
return mpc;
}
string Recv_Song()
{
return mpc;
}
Change line:
HSAMPLE sample = BASS_SampleLoad(FALSE, fi.Recv_Song().c_str, 0, 0, 1, BASS_SAMPLE_MONO);
to
HSAMPLE sample = BASS_SampleLoad(FALSE, fi.Recv_Song().c_str(), 0, 0, 1, BASS_SAMPLE_MONO);
c_str() is a function and you must call it as one.

Can we make array of sets in c++? [closed]

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 5 years ago.
Improve this question
Like the following way:
set<int> s[3];
I have tried it but it gives error in the line where I had tried to access its elements by writing s[i][j] where the error says
no match for 'operator[ ]'
The problem is not the array of std::set-s but rather the way you try to access elements inside your set.
std::set doesn't support operator [], that is why you are getting the error:
no match for 'operator[ ]'
Instead, access object using find() in the following way:
auto iter = s[i].find(<value>);
if (iter != s[i].end()) {
[..] // Do something with iter
}
Elements of a set are not accessed by index. s[i] is the (i-1)'th set but s[i][j] doesn't mean anything. You can check whether an element is present in the set using the find function. For eg s[i].find(3)!=s[i].end() . You can loop through the elements in sorted order using for(int x : s[i]){} (C++11 and above) or using iterators.

operand types are incompatible ("bool (*)()" and "bool") [closed]

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
I am trying to create a program in Visual Studio Community 2015 to play Tic-Tac-Toe. Sounds simple, not so much.
Anyway, I am using a function [bool playerCheck()] to make a switch for the function that checks whether the space that the user chooses is already taken.
void checkInput()
{
if (playerCheck == true)
Visual Studio gives the squiggly lines under the "==" and gives the message
operand types are incompatible ("bool (*)()" and "bool").
Not sure how the syntax should be for comparing function output to a constant.
playerCheck is a function which needs to be called in order to get the boolean result, i.e.:
if (playerCheck() == true)
bool (*)() is a type for a function which takes no arguments and returns a boolean.
playerCheck == true is an attempt to compare the function pointer with a boolean value, which leads to the compilation error which speaks for itself.
playerCheck() == true is calling the function and comparing the result (of type boolean) with the boolean value.
Note that if (x == true) can be generally abbreviated to if (x), so you could just write
if (playerCheck())

Error in Strings assignment. (C++) [closed]

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
I've looked everywhere and I've found one for php but not c++. I'm making a little console rpg for a project in my into to c++ class and I'd like to avoid functions and/or if possible--but if not I'll use them.
Sample code:
int main(){
string pName;
string pWpnName;
int pDamage=0;
int wdSwrdD=1;
if (pDamage==wdSwrdD)pWpnName="Wooden Sword";
cout<<"Please enter a name";
cin>>pName;
pDamage++:
cout<<"Name: "<<pName<<endl;
cout<<"Weapon: "<<pWpnName<<endl;
return 0}
But whenever I do this it outputs: Name: pName (like it's supposed to) and Weapon:. It just stays blank and I have a feeling it's something to do with how I'm using strings...
You do not understand basics of how imperative languages (and C++ is one of them) work. Program executed statement by statement, and your if condition checks pDamage==wdSwrdD only once - when execution flow goes through that statement. So the fact that you increase pDamage later will not magically change pWpnName (and you need to change comparison operator == to assignment operator = in that if condition in addition to that, but I assume this is a typo).
So you most probably need a loop where execution flow is repeatedly goes through your if statement (that's what loops are created for), but it is difficult to say anything more based on information you provided.
You can use the getline() function:
cout<<"Please enter a name"<<endl;
getline(cin, pName);
pDamage++;
The function can get a line from a stream, set std::cin as the steam argument, and assign a line input to a variable.
Your problem is that you've made a typo: == is equality comparison, while = is assignment. So, your section of code should be changed:
if (pDamage==wdSwrdD)
pWpnName=="Wooden Sword"; // here you're doing comparison
...to:
if (pDamage==wdSwrdD)
pWpnName="Wooden Sword"; // here you're doing assignment
Most compilers should generate a warning for this behavior because it's an easy typo to make, but can be difficult to catch.
In your program you have not initialized the strings and your are taking user input for pName and therefore it displays the name . In case pWpnName it's not initialized while declaring and the "if condition never becomes true" because you have initialized pDamage=0 and wdSwrD=1 and as we know if(0==1) is never true the string pWpnName never gets initialized to Wooden Sword so it displays blank.

If statement c++ with text [closed]

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
My code:
{
int reply;
cout<<"Am I doing something wrong: ";
cin>> reply;
if-part( reply == "yes") {
cout<<"Good";
}
}
Hi guys, I am newbie, I googled and youtubed the stuff, but i just can not find an answer.
Why is this code not running well if you text in if condition, but if you put number, everything is fine?
Thank you.
In your case you are comparing an integer with a pointer (address)
if-part( reply == "yes") {
Reply is a in value.
"yes" is a c-string, so it is roughly equivalent to
const char *yes = "yes"
where yes would be a pointer to the first byte in the string in y_e_s -- for example, 0x75243
so you are comparing an integer value to a pointer value such as 0x75243 and they are unlikely to ever be equal.
In java script this would work (better) since it converts types for you -- C++ does not, at least not like this.
reply is of type int and you are comparing it as a string.That's not possible.
You can simply use:
if (reply == 1)
cout << "Good\n";