-2

i made a simple code for a problem solving question but the result is weird for me here is the code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,num;
    string opr;
    cin>>n;
    num = 0;
    for(int i = 1; i <= n; i++){
         cin>>opr;
        if(opr.find("++"))
        {
            num+=1;
        }
        else if(opr.find("--"))
        {
            num-=1;
        }
    }

    cout<<num;
}

if i input n as number "1" it actually subtract and the result is -1 but if i inserted n as 2 or more it works fine what is happening?

  • If I understood correctly, you are simply trying to perform either increment or decrement n times. I'd encourage you to try and redo your code using different approach when it comes to choosing mathematical operation. – Apollo Dec 29 '19 at 21:41
  • i solved it by using: ,,, #include using namespace std; int main() { int n,num; string opr; cin>>n; num = 0; for(int i = 1; i <= n; i++){ cin>>opr; if(opr[1] == '+') { num++; } else if(opr[1] == '-') { num--; } } cout< – Ahmad Al Tawil Dec 29 '19 at 21:45
  • Nice if you already solved it, but just forget about [include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and consider to avoid [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Especially the combination of those two is inviting trouble in larger projects ;) – Lukas-T Dec 29 '19 at 21:53

2 Answers2

4

std::string::find doesn't work that way:

if(opr.find("++"))

you want:

if (opr == "++")

or if you just want to see if opr contains the substring "++":

if (opr.find("++") != std::string::npos)
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • sorry can u tell me what is string::npos mean? – Ahmad Al Tawil Dec 30 '19 at 12:34
  • @AhmadAlTawil It's the sentinel value used to indicate "no position" *i.e.* that `find` hasn't found it. So if `find` returns a position that isn't the "no position" marker, then we know it's found it. – Paul Evans Dec 30 '19 at 12:37
2

The problem is that std::string::find does not return a bool -- rather it returns the index at which it found the substring or std::string::npos if it doesn't find it. Since that is a number (actually a size_t) it can be implicitly converted to a bool and thus tested, but the result is not what you want. You need something like:

    if(opr.find("++") != std::string::npos)
    {
        num+=1;
    }
    else if(opr.find("--") != std::string::npos)
    {
        num-=1;
    }

if you are looking for the existence of a substring in the input.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    The diagnosis is correct but the suggested solution is less accurate than the other answer. – R Sahu Dec 29 '19 at 21:36
  • thank you for your replay...can you explain more about you analysis of the code? that is why i asked the question...the behavior was so weird and i didn't find a solution for it (as i said i already solved the problem its not hard) – Ahmad Al Tawil Dec 30 '19 at 12:35