Problem with analysing string


I've been studying programming from time to time and decided to make a text adventure, to practice what I already know. However, I have bumped into a problem with analysing a string that is determined by user input:

for( pos = i.begin(); pos != i.end() || pos != i.find( " " ); ++pos){
         iCheck += *pos;

This was supposed to look trough the string and put the first word into a seperate string:
The loop ends when the string is only one word long
pos != i.end()
or is supposed to end when it encounter a space in the string
|| pos != i.find( " " )
However, when trying to run my program I get the following error:

error: no match for 'operator!=' in 'pos != (&i)->std::basic_string<_CharT, _Traits, _Alloc>::find [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" "), 0u)'
I have not understood what you are trying to do but as for the error then pos has type std::string::iterator while the return value of member function find has type std::string::size_type. There is no such conversion function that converts an object of the first type into an object of the second type. So the compiler issued the error.
tbh, I'd just use boost split to do this for you:

    vector<string> split_words;
    boost::split(split_words, my_string_to_split, boost::is_any_of(" "));

.find() returns size_type and .begin()/.end() return an iterator. They are not compatible types to compare.

Topic archived. No new replies allowed.