Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:40:00+00:00 2026-05-31T02:40:00+00:00

I’ve been trying to split up an input string into smaller strings delineated by

  • 0

I’ve been trying to split up an input string into smaller strings delineated by whitespace. I found this code from here:

stringstream ss ("bla bla");
string s;

while (getline(ss, s, ' ')) {
cout << s << endl;
}

which works just fine. However, if I replace “bla bla” with a variable containing a string:

string userInput;
cin >> userInput;

stringstream ss (userInput);
string s;

while (getline(ss, s, ' ')) {
cout << s << endl;
}

only the first word/char/string prints out. Why is that? Is there a way to fix it? I’ve looked around at some stringstream questions, but the problem is that I don’t really know what I’m looking for.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:40:01+00:00Added an answer on May 31, 2026 at 2:40 am

    Your problem isn’t stringstream ss (userInput);, it’s the behavior of std::cin. Any whitespace will end the extraction of formatted user input, so the input bla bla will result in one std::string s = "bla" and another string "bla" waiting for extraction.

    Use cin >> noskipws >> userInput; instead. If you want to get a line, use std::getline(std::cin,userInput) instead. Have a look at this little demonstration, which compares std::getline to std::cin::operator>> on your input bla bla:

    Source:

    #include <iostream>
    #include <string>
    
    int main(){
            std::string userInput;
    
            std::cout << "Using std::getline(std::cin,userInput) on input \"bla bla\"." << std::endl;
            std::getline(std::cin,userInput);
            std::cout << "userInput contains \"" << userInput << "\"" << std::endl;
    
            std::cout << "std::cin >> userInput on input \"bla bla\"." << std::endl;
            std::cin >> userInput;
            std::cout << "userInput contains \"" << userInput << "\"" << std::endl;
            return 0;
    }
    

    Result:

    Using std::getline(std::cin,userInput) on input "bla bla".
    userInput contains "bla bla"
    std::cin >> userInput on input "bla bla".
    userInput contains "bla"

    See also:

    • std::getline from <string> (alternative resource).
    • noskipws (This will only prevent skipping leading whitespaces, a whitespace will still terminate the extraction).
    • istream::operator>>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.