Thread: Purpose of getline in C++?

  1. #1
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20

    Purpose of getline in C++?

    Hi, i'm new to C++ and the book which i am reading says that the purpose of getline is to show the whole line. Could someone please elaborate on this.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change "show" to "read" and it might make more sense to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    i still don't understand what it means... cant you read the whole line without getline?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Grae
    i still don't understand what it means...
    Suppose you want the user to enter a line of text, e.g.,
    Code:
    This is a line of text.
    You want to store the line read into a std::string object. You can thus write:
    Code:
    std::string line;
    std::getline(std::cout, line);
    Upon which the string will contain "This is a line of text."

    It is also possible to provide a third argument to getline to use a different delimiter, i.e., to read a "line" that stops when a character that you have in mind is read.

    Quote Originally Posted by Grae
    cant you read the whole line without getline?
    Yes, but getline makes it more convenient.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    k thanks that makes much more sense, this has nothing to do with the previous question but how would u compare two strings to see which comes first, using an if statement and comparison operators?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should start a new thread to ask a new question. But before you do that, read up more on how to use std::string objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    ok i shall

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  2. What is the purpose of C?
    By Davez69gto in forum C Programming
    Replies: 9
    Last Post: 01-27-2009, 01:09 PM
  3. My purpose, what do I do?
    By jaylc185 in forum Game Programming
    Replies: 5
    Last Post: 05-25-2005, 10:15 AM
  4. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  5. What is the purpose of a DLL?
    By Bluesun159 in forum C++ Programming
    Replies: 6
    Last Post: 07-12-2003, 02:44 AM

Tags for this Thread