1

I have written a function to parse string argument as a part of my project. Functionality is working all fine but I am getting one warning which says : C4018: '<' : signed/unsigned mismatch . I don't know why this warning is coming. Can I get some help here. Thanks!

void FileMgr::parseCmdLineForTextSearch(std::string b)
{
    int count=0;
    int flag = 0;
    patternVector.clear(); 
    for (int i = 0; i < b.length(); i++)   // this line where
 // the warning line comes


{
            if (b[i] == '"')
            {
                count++;
            }
        }
        if (count == 2)
        {
            for (int i = 0; i < b.length(); i++)
            {
                if (b[i+1] == '"')
                {
                    flag = 1;
                    tmp = b.substr(0, i+1);
                    tmp.erase(0, 1);
                    break;
                }
                else
                {
                    continue;
                }
            }
            std::istringstream iss(b);
            std::string word;
            while (iss >> word)
            {                  // for each word in b
                if (word.find("*.") == 0)
                {        // if it starts with *.
                    patternVector.push_back(word); // add it
                }
            }
            if (patternVector.size() == 0)
            {
                patternVector.push_back("*.*");
            }
            isCorrect = true;
        }
        else
            isCorrect = false;
    }
tanz
  • 627
  • 1
  • 10
  • 21
  • Fix it, I consider the warning as error (although your line will behave well, other might not) –  Feb 13 '15 at 18:51

2 Answers2

1

b.length() returns size_t which is unsigned. In your for loop, you are comparing a signed int i with the unsigned b.length(), which is why you see the warning. To get rid of it, use size_t i instead of int i when using i to indicate array indices.

Unrelated : You have an out of bounds access here if (b[i+1] == '"').

Pradhan
  • 16,391
  • 3
  • 44
  • 59
  • do I need to make changes in functionalities also. Or the result is same as int i – tanz Feb 13 '15 at 18:37
  • As long as `i` is just an array index, you are completely fine. Alternatively, you simply cast `b.length()` to `int` to avoid the warning. `for (int i = 0; i < (int)b.length(); i++)`. Admittedly, this looks a bit ugly :) – Pradhan Feb 13 '15 at 18:38
  • @Pradhan or start with unsigned i, in the first place –  Feb 13 '15 at 18:53
  • BTW, the reason comparing signed and unsigned values is an issue is because of **["The Usual Arithmetic Conversions"](http://stackoverflow.com/questions/7544123/c-usual-arithmetic-conversions)**. That is, before the comparison, a negative value signed int will be converted to an unsigned int interpretation of those same bits and result in a really large positive value. – Khouri Giordano Feb 13 '15 at 18:59
  • @DieterLücking Right, that's what I suggested in the answer. However, OP commented asking if this will need changes in the rest of his code. So if `i` really needs to be able to hold signed values... – Pradhan Feb 13 '15 at 19:15
  • @Pradhan : You rock! :) – tanz Feb 13 '15 at 19:49
0

You can avoid the unsignedand signed comparison non-sense by using std::string::iterator.

for ( auto iter = b.start(); iter != b.end(); ++iter )
R Sahu
  • 204,454
  • 14
  • 159
  • 270