No match for 'operator==' in a simple string code

荒凉一梦 提交于 2019-12-02 19:51:20

问题


Writing a simple code and ran into a problem I'm not sure how to deal with. I tried looking into it with a search but nothing I found was much help and everyone's answers were a little above my head. Someone please explain this like you would to a small child, haha. Thanks.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string invCode = "";
    string lastTwoChars = "";

    cout << "Use this program to determine color of furniture.";
    cout << "Enter five-character inventory code: ";
    cin >> invCode;

    if (invCode.length() == 5)
    {
        lastTwoChars = invCode.substr(3,2);
         if (lastTwoChars == 41)
         { 
              cout << "Red";
              }
         if (lastTwoChars == 25)
         { 
              cout << "Black";
              }
         if (lastTwoChars == 30)
         { 
              cout << "Green";
              }
    }
    else
         cout << "Invalid inventory code." << endl;

    system("pause"); 
    return 0;
}

回答1:


lastTwoChars is a string. You must compare it to a string, or at least a const char * or const char[].

The expression lastTwoChars == 41 compares lastTwoChars to 41--an int. This is not defined behavior for a string.

Instead, put 41 in quotes to make it a const char[] (specifically const char[3]):

 if (lastTwoChars == "41")

It looks like you do this several times in your code.




回答2:


Presumably, the error is complaining that you can't compare a string to a number. They are two different types, and unlike some languages there is no magic conversion (or comparison) between them.

You want to compare with another string:

if (lastTwoChars == "25")
//                  ^  ^



回答3:


lastTwoChars is a string, you are comparing it with an int in these statements :

         if (lastTwoChars == 41)
         { 
              cout << "Red";
         }
         if (lastTwoChars == 25)
         { 
              cout << "Black";
         }
         if (lastTwoChars == 30)
         { 
              cout << "Green";
         }

This is against the defined behaviour for string. You have to compare it to a string or char*.

         if (lastTwoChars == "41")
         { 
         }
              cout << "Red";
         .
         .
         .

Now "41" is a const char* in this case and it can be compared with a string or a char*.




回答4:


 #include <iostream>
 #include <string>

 using namespace std;

 int main()
 {
     string invCode = "";
     string lastTwoChars = "";

     cout << "Use this program to determine color of furniture.";
     cout << "Enter five-character inventory code: ";
     cin >> invCode;

     if (invCode.length() == 5)
     {
         lastTwoChars = invCode.substr(3,2);
          if (lastTwoChars == "fourty five") // you declared lastTwoChars as string therefore you have to compare letters not integers which are numbers.
          { 
               cout << "Red";
          }
     if (lastTwoChars == "twenty five") //same
     { 
          cout << "Black";
          }
     if (lastTwoChars == "thirty") // same
     { 
          cout << "Green";
          }
}
else
     cout << "Invalid inventory code." << endl;

cin.get(); // better than system("pause");
return 0;
}


来源:https://stackoverflow.com/questions/16177108/no-match-for-operator-in-a-simple-string-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!