CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2002
    Posts
    2

    reading text from the user

    I would like to know how to read a line of text, that the user inputs, into a string... I can't use cin because it needs to be able to work for more than one word. Thank you for any help!!

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    use getline() ...

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string line;
    
        cout << "Enter a line of input > ";
    
        getline(cin,line);
    
        cout << endl << "user input was > " << line << endl;
    
        return 0;
    }
    note : if using VC++, depending on the version, you might
    need to enter return twice after the user input. DinkumWare
    has a fix for this on their web page.

  3. #3
    Join Date
    Oct 2002
    Posts
    2
    I have a linux machine and getline is not working on it... I was wondering if getline might be for windows only or something. Thank you very much for any help.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    getine() is not Windows specific, so it should
    work under linux. I can test that tomorow.

    Are you using g++ ? Maybe try my test program
    and see if it works. If it works, but your code
    doesn't, maybe post your code.

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