Question Why is a space in the first input causing the first word to be stored in first string and second word in the second string? (C++)

Amol S.

Platinum Member
Mar 14, 2015
2,390
709
136
The second input should be storing in the second string.
Although the following is not the code I am having trouble with but does the same actions as the code I am having trouble with and gives the same issue. In regards to why I am sending it to vector then map then printing, that is the logic in the program I am having trouble with. However, my program is divided into multiple h and CPP files, thus the vector in my program acts like a data sender to the file responsible for inserting something into the map.


C++:
#include <map>
#include <string>
#include <iostream>
#include <vector>

using std::string;
using std::map;
using std::cout;
using std::cin:
using std::vector;

int main()
{
    map<string, string> theMap;
    vector<string> toAdd;
    toAdd.resize(2);
    vector<string>::iterator it = toAdd.begin();
    cout << "Enter first sentence.  \n";
    cin >> *it;
    it = std::next(it, 1);
    cout << "Enter second sentence.  \n";
    cin >> *it;
    theMap.insert({toAdd[0], toAdd[1]});
    for(map<string, string>::iterator it = theMap.begin(); it != theMap.end(); it++)
    {
        cout << it -> first << "          " << it -> second;
    }
}