Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to Use cin.fail() Method in C++?
Next article icon

How to Use the ignore() Function in C++?

Last Updated : 23 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the ignore() function is a part of std::basic_istream that is used to discard the characters in the stream until the given delimiter(including it) is reached and then extracts the left-out remainder. In this article, we will learn how to use the ignore() function in C++.

C++ ignore() Function

In C++, we can invoke the ignore() function with two arguments: the number of characters to discard and the delimiter character. This function discards characters from the input stream until a specified delimiter is encountered, including the delimiter itself or the specified number of characters have been ignored. The remaining characters in the stream are then extracted.

Syntax to use ignore() Function in C++

istream& ignore(size n, int delim = EOF);

Here,

  • n is the maximum number of characters to extract.
  • delim is the delimiter character.
  • EOF value for delim means that it will ignore characters until EOF is encountered or the limit set n is reached.

C++ Program to Demonstrate the Use of ignore() Function

The below example demonstrates the use of the ignore() function with cin to store only 3 numbers and discard any other user input.

C++
// C++ program to demonstrate the use of ignore()  #include <iostream> #include <limits> using namespace std;  int main() {      // infinite loop until user input is terminated.     while (true) {         // Declare variables to store input numbers.         int num1, num2, num3;          // Prompt the user to type numbers separated by         // spaces.         cout << "Type numbers separated by spaces: "              << endl;          // Read input numbers         cin >> num1 >> num2 >> num3;          // Check if the first number is 0 to exit the         // program.         if (num1 == 0)             exit(EXIT_SUCCESS);          // Ignore any remaining characters in the input         // buffer until newline character.         cin.ignore(numeric_limits<streamsize>::max(), '\n');          // Print the input numbers.         cout << num1 << "; " << num2 << "; " << num3              << endl;     }     return EXIT_SUCCESS; } 


Output

Type numbers seperated by spaces: 1 2 3 6 4 5 9 8 7
1; 2; 3
Type numbers seperated by spaces: 7 8 9 4 5
7; 8; 9
Type numbers seperated by spaces:

Time Complexity: O(1)
Auxilliary Space: O(1)

Explanation: In the above example we used numeric_limits<std::streamsize>::max() as the first argument of the ignore function for telling the function to ignore as many characters as possible until it reaches the delimiter.


Next Article
How to Use cin.fail() Method in C++?

K

kolisuszprv
Improve
Article Tags :
  • C++ Programs
  • C++
  • CPP-Functions
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

  • cin.ignore() Function in C++
    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 o
    3 min read
  • How to Create a Pointer to a Function in C++?
    In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use
    2 min read
  • How to Use cin.fail() Method in C++?
    In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp
    2 min read
  • Useful Inbuilt Functions in C++
    In-built functions in C++ are those functions that are part of C++ standard libraries. The purpose of inbuilt functions is to provide common and essential functionality that is frequently required in the programming. In this article, we will look at some of the commonly used inbuilt functions in C++
    7 min read
  • How to Overload the Function Call Operator () in C++?
    In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi
    2 min read
  • How to Use Deleted Functions to Prevent Object Copying in C++?
    In C++, the class always has a copy constructor and assignment operator, whether it is default or user-defined which allows the program to create copies of the objects of that class. But sometimes, we need to create a class whose object should not be copied. In this article, we will learn how to use
    2 min read
  • How to use errno in C++?
    In C++ errno is a preprocessor macro that is used for error indication and reporting errors that occur during a function call. It contains error codes so if a call to a function fails somehow then the errno is set to a value that corresponds to the error. errno is defined in the header file <cerr
    4 min read
  • How to Use the Not-Equal (!=) Operator in C++?
    The not-equal operator is a fundamental comparison operator in C++ represented by "!=". It is used for making decisions in programming and is hence called a conditional operator. In this article, we will discuss how to use the Not-Equal (!=) operator in C++. Not-Equal (!=) Operator in C++The not-equ
    3 min read
  • How to Utilize the goto Statement in C++?
    The goto statement in C++ is a control flow statement that allows the users to move the control flow from one part to another part of the program. In this article, we will learn how to utilize the goto statement in C++. Utilize the goto Statement in C++In C++, the goto statement transfers the progra
    2 min read
  • How to Read a File Using ifstream in C++?
    In C++, we can read the contents of the file by using the ifstream object to that file. ifstream stands for input file stream which is a class that provides the facility to create an input from to some particular file. In this article, we will learn how to read a file line by line through the ifstre
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences