0

hey I want to input a string that contain spaces such as "My name is Shankhs";
i initialized a string str;
then cin>>str;
but str is not taking words after spaces as if spaces are acting as a delimiter...
i tried gets,getline but they does not seem to work for string!!!
So guys what should I do to make string take words after spaces???
(P.S. I have to work with string no char [] or char * please)
Thanx for your concern

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by shankhs
1

hey I want to input a string that contain spaces such as "My name is Shankhs";
i initialized a string str;
then cin>>str;
but str is not taking words after spaces as if spaces are acting as a delimiter...
i tried gets,getline but they does not seem to work for string!!!
So guys what should I do to make string take words after spaces???
(P.S. I have to work with string no char [] or char * please)
Thanx for your concern

getline should work fine. Check out the example on this link. Type in a first and last name with a space in between at the prompt and it will read in the whole string (first + space + last). Spaces are indeed delimiters when you use >>.
http://www.cplusplus.com/reference/string/getline.html

0

thanx i implemented getline wrong so i was getting wrong answer thak you very much

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.