Thread: getline vs cin

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    getline vs cin

    Hi everyone, I am new c++ learner and I am watching a series of youtube right now.

    Watching this tutorial
    YouTube

    I cannot understand the reason that the tutor uses string to store information for age (using the getline function to pass information from the user) and then he converts the string into integer to make some comparisons..

    It wouldn't be much simpler to use int as data type for age and then use cin >> age; to receive data from the user?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nikosant03
    It wouldn't be much simpler to use int as data type for age and then use cin >> age; to receive data from the user?
    Yes, it would be simpler if that were all the input that you intended to read, but you might want to read an integer (using the overloaded operator>>), then read a string (using getline), and then you need to worry about the trailing newline left from reading the integer before you read the string, otherwise the getline call will end up reading an empty string because it will read and find that it encounters the newline left behind before it reads the input you actually want to read. Hence, if you read entire lines into strings and parse them as needed, you can avoid having to worry about such trailing newlines. It can also be helpful in dealing with parse errors: you don't have to worry about whether there might be any other junk left on the line entered as you have already read all of it and so you can just discard it and prompt the user to try again with a brand new line.
    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
    Join Date
    Feb 2019
    Posts
    97
    Thank you for your answer!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  2. help with getline.
    By Nicheter in forum C Programming
    Replies: 2
    Last Post: 02-11-2008, 03:35 PM
  3. Getline help!
    By really_bad_prog in forum C++ Programming
    Replies: 10
    Last Post: 06-23-2006, 03:49 PM
  4. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM

Tags for this Thread