![]() |
VOOZH | about |
The cin.ignore() function in C++ is a member function of the std::istream. It is used to ignore (or discard) certain number of characters from the input buffer. This function is very useful when we have to deal with leftover characters in the input stream that could interfere with subsequent input operations, such as newlines or other delimiters.
Syntax:
cin.ignore(count, delimiter);
The cin.ignore() function in C++ accepts the following two parameters:
The cin.ignore() function does not return any value. Its main purpose is to discard characters from the input buffer up to the specified limit.
In the example below, we used cin.ignore() to discard the newline character left in the input buffer after reading the integer age, ensuring that the next input (for initial) is handled correctly.
Output
Enter your age: 10
Enter the first letter of your name: G
Your age is 10 and your initial is GIn the above example, cin.ignore(10, ' ') is used to skip up to 10 characters or until a space is encountered, that allows the program to process the next meaningful input.
Output
Enter a sentence: Hellowelcometo geeksforgeeks
Next character after ignoring: m