When to use cin.ignore () in C ++?

5

What is the exact time to use cin.ignore() in software written in C ++? Why a lot of the time, when I'm doing a big software, the readings are bugging, sometimes if I put cin.ignore() debugging, sometimes it buga more.

So, what's the right way to use cin.ignore() ?

    
asked by anonymous 02.08.2018 / 20:17

1 answer

7

As the name itself suggests, cin.ignore() is used when you want to ignore one or more input buffer characters.

  

What is the exact time to use cin.ignore () in software written in C ++?

The cin.ignore() treats as the delimiter the characters: \t and \n , then you will use when you receive an entry and you want to ignore those characters that remained in the input buffer .

  

So, what's the right way to use cin.ignore ()?

In summary, you use cases that you want to ignore unread characters in the input buffer , as I mentioned earlier.

Ex:

  • You want to get a string of the user and store in a char :

    char teste[20];

  • Read the entry using cin :

    cin >> teste;

  • Assuming the user entered teste and pressed ENTER. The teste will be received, and \n will remain in the input buffer.

  • I tried to be as brief as possible in the answer, this reference can be a complement to your understanding.

        
    02.08.2018 / 20:56