Thread: Is there anyway to use the getline function with strings?

  1. #1
    Terranc
    Guest

    Is there anyway to use the getline function with strings?

    Is there anyway to use the getline() function with strings seperated by \n or endl???

    Thanks,
    Terrance

  2. #2
    Terranc
    Guest
    sorry, incomplete post.

    i.e., could I turn a string into a stream, and use the getline function like this:

    getline(stringToStream, stringName);

    Is there anyway to turn a string into a stream, seperated by the \n, endl;

    Thanks

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by Terranc
    i.e., could I turn a string into a stream, and use the getline function like this:

    getline(stringToStream, stringName);

    Is there anyway to turn a string into a stream, seperated by the \n, endl;

    Thanks
    Yes, there is. Here's how the apstring class does that.
    Code:
    istream & getline(istream & is, apstring & str)
    {
    	char ch;
    	str = "";
    
    	while(is.get(ch) && ch != '\n')
    		str += ch;
    	return is;
    }
    Now, you're probably going to have to finagle that to get it to work with the type of string you're using. To use the function above, call it like so:
    Code:
    getline(cin, string_variable);
    If you need help getting that to work with your type of string(char[], string, etc.), just let us know what that is, post your code and a specific question, and we'll try to help you out.

    I hope that answered your question
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i.e., could I turn a string into a stream, and use the getline function like this:
    It sounds like you want a stringstream:
    Code:
    #include <iostream>
    #include <sstream>
    
    int main()
    {
      std::string s = "Stuff up to the newline\nJunk after the newline";
      std::string a;
    
      std::istringstream iss ( s );
    
      std::getline ( iss, a );
    
      std::cout<< a <<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    terrancee
    Guest

    Smile

    thanks for the help, got my thing to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM