user1745860
user1745860

Reputation: 247

getline delimiter using stringstream

It seems that it's not separating the word within the space.

Trying to separate the words in between, and stored it in first and second.

cin >> name; //input name 
stringstream file (name);
getline(file,first, ' '); //seperate the name with the first name and last name using space 
getline(file,second, ' ');

Upvotes: 0

Views: 5696

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490138

When you read the initial input with cin >> name; that only reads up to the first white space character.

You then try to break that into two pieces at white space, which it doesn't contain.

Easy way:

cin >> first >> second;

Alternatively, if you start with std::getline(cin, name); instead of cin >> name;, then the rest should work correctly.

Upvotes: 2

user93353
user93353

Reputation: 14039

Replace

cin >> name;

with

getline(cin, name); //input name

cin >> reads only upto the first space. You would have realized this if you done a cout << name; to check what's getting read - this is the first step of debugging.

Upvotes: 3

Related Questions