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

前端 未结 4 989
挽巷
挽巷 2021-01-28 18: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 wer

4条回答
  •  抹茶落季
    2021-01-28 18:33

     #include 
     #include 
    
     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;
    }
    

提交回复
热议问题