What am I doing wrong?

I set up an if statement like this and buffer_start_pos is equal to int 1 when I pass it to Title.Set_Zone_Name.

1
2
3
4
    if (buffer_start_pos = buffer.find("You have entered ")!= std::string::npos){
            Title.Clear_Zone_Name();
            Title.Set_Zone_Name(buffer_start_pos,buffer);
            }


but if I set up my if statement like this everything works fine.

1
2
3
4
5
    if (buffer.find("You have entered ")!= std::string::npos){
            buffer_start_pos = buffer.find("You have entered ");
            Title.Clear_Zone_Name();
            Title.Set_Zone_Name(buffer_start_pos,buffer);
            }


I thought you could set a variable equal to something in an if statement without interfering with the condition check?

edit.. nvm. I figured it out. It was setting buffer_start_pos to 1 because the if statement was true.
Last edited on
closed account (j3Rz8vqX)
I'm not entirely sure, but just guessing that buffer_start_pos is of integer type whereas std::string::npos is a size_t type data.

Integer and size_t data types look similar and can interact nicely due to overloaded operators in size_t, but may actually operate differently if added to the stack.

Integer type data is a primitive, clean and simple.
size_t is a more advance data type that is used mainly as an index.
http://www.cplusplus.com/reference/cstring/size_t/

Most of the time, they are compatible in assignment but in the case of an if, there may be some magic going on in the conversion of size_t to int that is possibly throwing a flag.

Possibly more to it than this, just how I've come to understand size_t versus integer.

Last edited on
Try wrapping the assignment in parentheses if ((buffer_start_pos = buffer.find("You have entered "))!= std::string::npos){
ahh thanks.

That seems to make it work :)
Topic archived. No new replies allowed.