
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of cin.ignore() in C++?
In C++, cin.ignore() is a built-in function that belongs to the istream class and is defined in the <iostream> header.
It is used to skip characters left in the input buffer, especially the newline character ('\n'), which can cause issues with input operations like getline().
In this article, we will explain the use of cin.ignore() in C++.
Syntax of cin.ignore()
Below is the syntax of the cin.ignore() function.
cin.ignore(numeric_value, delimiter_char); // Example: cin.ignore(1000, '\n');
Here, numeric_value is the maximum number of characters to skip, and delimiter_char is the character at which the input should stop being ignored. If not specified, cin.ignore() by default skips one character.
Using cin.ignore() with getline()
This example shows how to properly handle input when using cin >> followed by getline(). Since cin >> leaves a newline in the buffer, getline() might read it as an empty line. Using cin.ignore() clears that newline and allows getline() to read the actual input.
C++ Program using cin.ignore() with getline()
Below is the C++ program where we first take an integer using cin >> and then read a full line using getline(). cin.ignore() is used between them to clear the newline.
#include <iostream> #include <string> int main() { int age; std::string name; std::cout << "Enter your age: "; std::cin >> age; // Use cin.ignore() to clear the leftover newline character std::cin.ignore(); // Ignores the newline character left by the last input std::cout << "Enter your name: "; std::getline(std::cin, name); // Now this works as expected std::cout << "Your age is: " << age << std::endl; std::cout << "Your name is: " << name << std::endl; return 0; }
Below is the
Enter your age: 24 Enter your name: Arvik Your age is: 24 Your name is: Arvik
Skipping Multiple Characters Using cin.ignore()
You can also use cin.ignore() to skip a specific number of characters in the buffer. This is useful when you want to skip unwanted input.
C++ Program to Skip Multiple Characters
Below is the program where we use cin.ignore(5) to skip 5 characters from the input buffer and then wait for the next key press.
#include <iostream> #include <string> int main() { std::string sentence; std::cout << "Enter a sentence: "; std::getline(std::cin, sentence); std::cout << "You entered: " << sentence << std::endl; std::cout << "Press a key to continue..."; std::cin.ignore(5); // Skip next 5 characters in input buffer char ch; std::cin >> ch; // Waits for next key press return 0; }
Below is the output where the user enters a sentence, and it is displayed back. Then the program skips 5 characters and waits for a key press.
Enter a sentence: Hii, Welcome to tutorialspoint!! You entered: Hii, Welcome to tutorialspoint!! Press a key to continue...