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

  • Home
  • SEARCH
  • 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 9135273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:48:10+00:00 2026-06-17T08:48:10+00:00

I recently bought a C++ Primer and got stuck with a problem. I have

  • 0

I recently bought a C++ Primer and got stuck with a problem. I have to read a sequence of words using cin and store the values in a vector. After having unusual problems, I found out that while(cin >> words) invites problems (like infinite loop) if you expect invalid inputs: Using cin to get user input

int main()
{
    string words;
    vector<string> v;
    cout << "Enter words" << endl;
    while (cin >> words)
    {
        v.push_back(words);
    }
    for(auto b : v)
        cout << b << "  ";
    cout << endl;
    return 0;
}

Therefore, I’m trying to find an alternative to this problem. Help ?

  • 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-06-17T08:48:10+00:00Added an answer on June 17, 2026 at 8:48 am

    That link you provided regarding input problems is a little different. It’s talking about when you expect the user to enter a particular value, but you might fail to read the value (let’s say it’s an integer) because something else was entered. In that case, it’s good to use getline to retrieve a whole line of input and then parse the value out.

    In your case, you’re just after words. When you read a string from a stream, it will give you all consecutive non-whitespace characters. And, ignoring punctuation for a moment, you can call that a “word”. So when you talk about ‘invalid input’, I don’t see what you mean. The loop will continue to give you “words” until there are none left in the stream, at which point it will error:

    vector<string> words;
    string word;
    while( cin >> word ) words.push_back(word);
    

    However, if you expect the user to enter all words on one line and press enter to finish, then you need to use getline:

    // Get all words on one line
    cout << "Enter words: " << flush;
    string allwords;
    getline( cin, allwords );
    
    // Parse words into a vector
    vector<string> words;
    string word;
    istringstream iss(allwords);
    while( iss >> word ) words.push_back(word);
    

    Or you can do this:

    cout << "Enter words, one per line (leave an empty line when done)\n";
    
    vector<string> words;
    string line;
    while( getline(cin, line) )
    {
        // Because of the word check that follows, you don't really need this...
        if( line.size() == 0 ) break;
    
        // Make sure it's actually a word.
        istringstream iss(line);
        string word;
        if( !(iss >> word) ) break;
    
        // If you want, you can check the characters and complain about non-alphabet
        // characters here...  But that's up to you.
    
        // Add word to vector
        words.push_back(word);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently bought VPS hosting and I assume, that it has problem with
I got a problem. I recently bought an Arduino Uno board. I tried to
I have recently started using LinqPad, and bought the Autocomplete option and am really
I recently bought some webspace with dailyrazor.com. I have deployed an application I have
I bought an iPod touch 3rd Gen half year ago, and I got recently
We recently bought two new build machines that have the same specs/hardware. From what
Here's the scenario: Our company recently bought an Enterprise Developer Account. We already have
I program in Asp.Net, C#, and I have recently bought a tablet with android
I've recently bought a new computer and as such have had to install visual
I recently bought Programming Scala , and have been reading through it. The language

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.