last word of string ignored

Hi all,
I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>
   
std::vector < std::string > MakeVector( std::string str );
   
int main( int argc, char* argv[] ) {
    std::string str = "Hello, how are you?";
    std::vector < std::string > vec = MakeVector( str );
     
    for ( int i = 0; i < vec.size(); i++ ) {
        std::cout << vec[ i ] << " ";
    }
      
    return 0;
}
   
std::vector < std::string > MakeVector( std::string str ) {
    std::string temp;
    std::vector < std::string > vec;
        
    for ( int i = 0; i < str.size(); i++ ) {
        if ( str[ i ] != ' ' ) {
            temp += str[ i ];
        }
        else if ( str[ i ] == ' ' ) {
            vec.push_back( temp );
            temp = "";
        }
    }
        
    if ( vec.size() == 0 ) {
        vec.push_back( str );
    }
        
    return vec;
}

It is supposed to divide the string in words, store each word in a vector and then iterate through the vector and output each word. The problem is, the output I get here is "Hello, how are".
Any help?
Thanks!
Thanks! :)
Topic archived. No new replies allowed.