Previous Article Next Article C++ Extraction Operator, getline and cin.ignore()
Posted in C++

C++ Extraction Operator, getline and cin.ignore()

C++ Extraction Operator, getline and cin.ignore() Posted on May 17, 201829 Comments



Learn how the extraction operator and the getline function handles keyboard input and how the cin object allocates data into memory. Also learn how to use cin.ignore() to discard characters from the input stream.

Table of Contents:

02:22 – The cin object
03:25 – getline() function
07:58 – ignore()

29 comments

  1. profgustin!! ..your tutorial is awesome, my lecturer never explain the ignore function to me an i was facing difficulties with it but am glad that you explain it in a professional way thanks million times prufgustin.. i never subscribe in you tube tutorials but i have to start with you, two tombs up for you..

  2. Prof Gustin, excellent video. Your explanation makes everything clear, ten million times clearer than people on Stack Overflow. This is suited for beginners for sure! Thank you so so so very much!

  3. Thank you for clearing up how this works. I was having issues with a loop; the first run cin would ask for input, but on the second run it wouldn't ask for the input and it would be an infinite loop. I ended up finding a solution online but your method works too (and it's less code 😀 ).

    online suggested;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

    which did work. using <limit> header.

    I think the book i'm following talks about cin.ignore but not in as much detail as you did.

  4. i am confused about last part of the video. If we write 5 at the first argument of ignore and write  "n" at the second argument of ignore, what will happens ?
    Does the function ignore discard 5 characters or all of characters until the second argument(n) ?

  5. I'm trying to do an if function such as "What is your name?" "prof gustin"
    if (name == "prof gustin") {
    cout << "nGreeting my good friend!";
    So if someone inputs a certain name I want a certain response.
    the getline and cin.ignore lines seem to be preventing this

Leave a Reply

Your email address will not be published. Required fields are marked *