CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    32

    How to input white space with type std::string?

    Hi all,
    When the following code is executed, we cannot input any string with white-space.
    What if I actually want to input several words separated by white-spaces? How should I do that

    Thanks in advance

    Code:
    string temp;
     cin>>temp;

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to input white space with type std::string?

    Use the version of std::getline for std::string.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How to input white space with type std::string?

    Alternatively, you can also use the noskipws manipulator. I'd recommend the getline method though.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Dec 2010
    Posts
    32

    Re: How to input white space with type std::string?

    How to use the getline?
    I tried
    Code:
            string temp;
    	cin.getline(temp,20);
    Should I include a header file to make that work?

  5. #5
    Join Date
    Dec 2010
    Posts
    32

    Re: How to input white space with type std::string?

    OK, thanks!
    I tried
    Code:
    getline(cin,temp)
    And it worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured