Thread: Why do we use cin.ignore()

  1. #1
    Whats gonna happen now! himanch's Avatar
    Join Date
    Feb 2005
    Location
    Armenia
    Posts
    26

    Why do we use cin.ignore()

    i could not understand why do we use cin.ignore() and cin.get()!!
    as i understand from the tutorials if we write cin.get() this code prevents the writing to show up and disappear but when i merely use get.cin() without cin.ignore it does not work as i wish.


    . ...............Please Help.........

  2. #2
    Hello,

    According to the C++ Library Reference.

    ignore():
    Code:
    istream&  ignore ( streamsize n = 1, int delim = EOF );
    Extracts characters from input stream and discards them. Extraction ends when n characters have been discarded or when delim is found, whichever comes first. In this last case delim is also extracted.

    get(): Extracts a character from the stream and returns its value.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    As for the specific instance when you use it, that's because the console window a console application opens vanishes the moment the console app closes. Unless some other app holds it open.
    The cin.get() reads a single character from the user and waits for that, thus keeping the application running. Since get() might also just read a character that's waiting in the input buffer, ignore() is first called to make sure there's nothing in the input buffer.

    A better way of solving the problem would be to finally make Dev-C++ as smart as Visual C++ and have it open a small wrapper application, which holds the console window open. I just don't get why it still doesn't do that.

    A batch file would suffice!
    Code:
    rem This file launches an app and keeps the console open.
    shift
    %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
    pause
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.ignore()
    By valthyx in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 04:23 AM
  2. Console window waiting on cin.ignore() or cin.ignore(2)
    By The SharK in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2006, 04:17 PM
  3. Can't seem to come to conclusion : cin.ignore()
    By SkyHi in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2005, 08:57 PM
  4. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 01:14 AM
  5. cin.ignore
    By Honderman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2002, 09:51 PM