how to finish the input?

Discussion in 'C++' started by asdf, Sep 20, 2006.

  1. asdf

    asdf Guest

    string text_word;
    while (cin>>text_word)
    vector1.push_back(text_word);

    I just want to input two strings, how to terminate the input ? I press
    Enter key, but it didn't terminate input.
     
    asdf, Sep 20, 2006
    #1
    1. Advertising

  2. asdf wrote:

    > string text_word;
    > while (cin>>text_word)
    > vector1.push_back(text_word);
    >
    > I just want to input two strings, how to terminate the input ? I press
    > Enter key, but it didn't terminate input.


    The "while" construct is for loops. You don't need a loop in this
    program. Why are you using "while"?

    Best regards,

    Tom
     
    Thomas Tutone, Sep 20, 2006
    #2
    1. Advertising

  3. asdf wrote:

    > string text_word;
    > while (cin>>text_word)
    > vector1.push_back(text_word);
    >
    > I just want to input two strings, how to terminate the input ? I press
    > Enter key, but it didn't terminate input.


    This depends on the platform you are using. Under Windows you have press
    Ctrl + Z, under Unix Ctrl + D to issue an EOF.

    Regards,
    Stuart
     
    Stuart Redmann, Sep 20, 2006
    #3
  4. asdf

    Default User Guest

    asdf wrote:

    > string text_word;
    > while (cin>>text_word)
    > vector1.push_back(text_word);
    >
    > I just want to input two strings, how to terminate the input ? I press
    > Enter key, but it didn't terminate input.



    That because newline is just another white space to >>. My
    recommendation is to use some sort of counting loop.


    void GetStrings(std::vector<std::string> &v, int n)
    {
    std::string s;
    char c;

    std::cout << "> ";

    for (int i = 0; i < n; i++)
    {

    std::cin >> s;
    v.push_back(s);
    }

    std::cin.get(c); // eats the newline hanging out
    }



    Brian
     
    Default User, Sep 20, 2006
    #4
  5. asdf

    Default User Guest

    Thomas Tutone wrote:

    > asdf wrote:
    >
    > > string text_word;
    > > while (cin>>text_word)
    > > vector1.push_back(text_word);
    > >
    > > I just want to input two strings, how to terminate the input ? I
    > > press Enter key, but it didn't terminate input.

    >
    > The "while" construct is for loops. You don't need a loop in this
    > program. Why are you using "while"?


    Yes, he does. He wants to read two words out of one input line.



    Brian
     
    Default User, Sep 20, 2006
    #5
  6. asdf

    Default User Guest

    Stuart Redmann wrote:

    > asdf wrote:
    >
    > > string text_word;
    > > while (cin>>text_word)
    > > vector1.push_back(text_word);
    > >
    > > I just want to input two strings, how to terminate the input ? I
    > > press Enter key, but it didn't terminate input.

    >
    > This depends on the platform you are using. Under Windows you have
    > press Ctrl + Z, under Unix Ctrl + D to issue an EOF.



    This sort of answers the OP's question, but a platform-independent
    solution is probably better.




    Brian
     
    Default User, Sep 20, 2006
    #6
  7. asdf

    Default User Guest

    Thomas Tutone wrote:

    > asdf wrote:
    >
    > > string text_word;
    > > while (cin>>text_word)
    > > vector1.push_back(text_word);
    > >
    > > I just want to input two strings, how to terminate the input ? I
    > > press Enter key, but it didn't terminate input.

    >
    > The "while" construct is for loops. You don't need a loop in this
    > program. Why are you using "while"?



    I had another reply to your message that I canceled, however it may
    show up anyway. This is a more accurate reply.

    A loop is one way of doing it, I presented one in another message.
    Obviously another way is to have repeated code. That's not the most
    efficient way in general, although for two it probably doesn't make
    that much difference.




    Brian
     
    Default User, Sep 20, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Smedley
    Replies:
    1
    Views:
    297
    Steven Cheng[MSFT]
    Feb 14, 2004
  2. Miguel Dias Moura
    Replies:
    1
    Views:
    363
    Cor Ligthert
    Apr 12, 2004
  3. Miguel Dias Moura
    Replies:
    2
    Views:
    352
    Miguel Dias Moura
    Jun 19, 2004
  4. tony lincoln
    Replies:
    2
    Views:
    350
    John C. Bollinger
    Oct 16, 2003
  5. neu
    Replies:
    2
    Views:
    308
    VisionSet
    Nov 15, 2003
Loading...

Share This Page