for (int i = 0; i < word.length(); i++)
{
if (&word == "<")
{
inTag = true;
} else if (&word == ">")
{
inTag = false;
}
}
Why won't my if statement work!?
Assuming that word is a std::string, the main issue is that you're taking the address of a character at position i, and then attempting to compare it to a string. While this may compile, it won't do what you're expecting. Essentially, you're just comparing two random locations in memory to see if they are equal.
To make this correct code, we must make two main modifications.
1: We are not comparing the memory location of the characters, so we need to drop the & operator.
2: We are not comparing a string to another string, we are comparing a character to another character. So, we need to replace "<" and ">" (String literals) with '<' and '>' (Character literals).
To make this correct code, we must make two main modifications.
1: We are not comparing the memory location of the characters, so we need to drop the & operator.
2: We are not comparing a string to another string, we are comparing a character to another character. So, we need to replace "<" and ">" (String literals) with '<' and '>' (Character literals).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement