Thread: getline function with string class

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    getline function with string class

    can the getline function be used with a string class object?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    if you are speaking of a string.h file and you declare a string as String aString..then at school we used this..but i dont like it....

    Code:
    #include <lvp\string.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    String something;
    getline(cin, something);
    return 0;
    }
    that only works for some...i have never really implemented any strings in any of my code..i always think of them as array of characters like

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char* something = new char[100];
    	cin.getline(something, 100, '\n');
    	
    	delete [] something;
    	return 0;
    }
    nextus, the samurai warrior

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can the getline function be used with a string class object?
    Yes, getline for std::string's is implemented in <string>:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string s;
    
      if ( std::getline ( std::cin, s ) )
        std::cout<< s <<std::endl;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM