0

I have trouble understanding what's exactly happening when i put .find() function inside if condition. Similar call in for loop seems to work good.

Part of my code goes like this:

    size_t endPos = strbuf.find(endSeq)+endSeq.size();
    cout<<endPos<<endl;

    if (size_t endPosif = strbuf.find(endSeq)+endSeq.size() != string::npos)
    {
        cout<<endPosif<<endl;
    }

    for (size_t endPosfor = strbuf.find(endSeq)+endSeq.size();endPosfor != string::npos; 
        endPosfor=strbuf.find(startSeq,endPosfor+1)+endSeq.size())
    {
        cout<<endPosfor<<endl;
    }

Output:

466
1
466

What causes this behavior? I would like to use endPosif inside if statement.

Rafael
  • 25
  • 5
  • You should do one thing at a time, or at least use `()` to enforce correct order of operations. Also note that result of `strbuf.find(` must be checked to figure out whether it did found anything *prior* to any other use. – user7860670 Apr 16 '20 at 12:14
  • This is wrong: `strbuf.find(endSeq)+endSeq.size() != string::npos` condition will always pass. Same `endPosfor != string::npos` since it is set to `endPosfor=strbuf.find(startSeq,endPosfor+1)+endSeq.size()`. – Marek R Apr 16 '20 at 12:15
  • @MarekR Right. It's just example if statement I did to show what's my problem is. – Rafael Apr 16 '20 at 12:19
  • So please provide [mcve] you can use this site: https://wandbox.org/ to provide such example – Marek R Apr 16 '20 at 12:19
  • @user7860670 When I have ( ) like these ` if (size_t endPosif = (strbuf.find(endSeq)+endSeq.size()) != string::npos) ` it's still the same result – Rafael Apr 16 '20 at 12:21
  • @MarekR I would like to know why my variable equals 1 inside statement. That was my question – Rafael Apr 16 '20 at 12:27

1 Answers1

0

I would like to know why my variable equals 1 inside statement. That was my question

if (size_t endPosif = strbuf.find(endSeq)+endSeq.size() != string::npos)

this strbuf.find(endSeq)

  • returns string::npos (which is like -1) when nothing is found
  • or something in ragne <0, strbuf.size() - endSeq.size()>

When you add to that endSeq.size() (what is never zero) result is always different then string::npos.

So this strbuf.find(endSeq)+endSeq.size() != string::npos is evaluated to true.

true is converted to 1 when cast to size_t.

So you have two bugs:

  • incorrectly handling scenario when nothing is found
  • evaluation order (that is why user7860670 wrote about parenthesis)
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • When I delete part `+endSeq.size()` endPosif still equals 1. I read this post (https://stackoverflow.com/questions/17681535/variable-assignment-in-if-condition) so I assumed I can do initialization, assigment and condition check in one step, so i dont unerstand why this syntax doesn't work. At the moment I have workaround that looks like this `if (size_t endPosif = strbuf.find(endSeq); endPosif != string::npos)` and works fine. – Rafael Apr 16 '20 at 12:48
  • Ok. I read (https://stackoverflow.com/questions/17681535/variable-assignment-in-if-condition) again and i found this: _The only drawback with this approach is that you can't use it when the variable is declared in the condition. That is, you cannot rewrite:_ `if (int x = Foo()) { ... }` as `if ((int x = Foo()) != 0) { ... }` . So i think this is answer to my question. Tank you for help either way – Rafael Apr 16 '20 at 13:02