Why do we use cin.ignore()

This is a discussion on Why do we use cin.ignore() within the C++ Programming forums, part of the General Programming Boards category; i could not understand why do we use cin.ignore() and cin.get()!! as i understand from the tutorials if we write ...

  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
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    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,893
    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, 05: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, 05: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, 09:57 PM
  4. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 02:14 AM
  5. cin.ignore
    By Honderman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2002, 09:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21