Hello.

Here is a snippet:

#include "stdafx.h" #include <iostream> #include <string> std::string MainStr = "Word1:Word2:Word3:Word4:Word5"; std::string S = ":"; short X = 0; for (int i = 0; i < MainStr.length(); i++) { if (MainStr[i] == S) { X = X + 1; } } 

For some reason, he writes a binary '==' : no operator found which takes a left-hand operand of type 'char' (or there is no acceptable conversion) error binary '==' : no operator found which takes a left-hand operand of type 'char' (or there is no acceptable conversion) in the condition string.

What's wrong?

  • Well, about the error already written, but why not use the find_first_of method to find the desired character? size_t pos = 0; for (X = 0; (pos = MainStr.find_first_of (':', pos))! = string :: npos; pos ++, X ++); - alexlz


2 answers 2

So MainStr is not an array, and it’s not so easy to run over it ... And then, in the same char, you need to replace the quotes with single

  • 2
    Well, maybe not an array, but it behaves like an array (this is in fact a vector - std :: vector) - KoVadim
  • 2
    And very much run. And even in several ways. - skegg

Because MainStr[i] is of type char . We declare correctly and rejoice instead

 std::string S = ":"; 

write:

 char S = ':'; 
  • And what is the difference between single and double? - BlackOverlord
  • 2
    Well, single for characters, double for strings. Read the tutorial carefully - KoVadim
  • single is one character of type char, and double is a constant of type const char * - ProkletyiPirat
  • @ProkletyiPirat - two - skegg
  • one
    1. Not “you”, but “you” 2. If you make such mistakes, it means you don’t understand anything 3. Not “found”, but “found”. Again deuce. And in general, this is the string C, which is an array of charms, ending with the symbol '\ 0' - skegg