What is cin.ignore()?

This is a discussion on What is cin.ignore()? within the C++ Programming forums, part of the General Programming Boards category; I know this has probably alredy been discussed but i dont understand how cin.ignore() works exactly and if someone could ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    What is cin.ignore()?

    I know this has probably alredy been discussed but i dont understand how cin.ignore() works exactly and if someone could explain that to me.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    cin represents a console input buffer. When you use cin to read from the console it checks its buffer to see if there are any characters there. If there are not, then it gets the next characters from the console. Generally, the characters it retrieves from the console are everything up until the user hits enter. So for example, if your code attempts to read only a single integer from cin, and the user types the integer and presses enter, then the integer and a newline from the enter are both stored in the buffer for cin. The integer is then read into your variable, but the newline is left in the buffer. You can call cin.ignore() to ignore that newline so that it doesn't mess up your code when you use cin again later.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    ohh ok so all it tells the program to do basicly is " Ok take this number but jsut ignore the enterbutton"?

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    No. Read the link given.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Ok take this number but jsut ignore the enterbutton
    That was just an example, it doesn't have to be a newline. The function ignore() means ignore the next character in the buffer (or if the buffer is empty wait for the user to type more stuff and ignore the first character typed). The most common use for ignore() in beginner programs is to ignore the enter after reading in a number, but the function itself doesn't care what character it ignores, it just ignores whatever the next character in the buffer is.

    The ignore() function also has optional parameters so that you can ignore more than one character. This is most commonly used to ignore everything in the input buffer. For example (remember, this is just an example), if you ask for an integer, and the user types a word, then your code will have to clear the error state of cin and ignore the word the user typed before giving a warning and asking the user to try again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. 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
  3. Why do we use cin.ignore()
    By himanch in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2005, 12:52 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, 08:51 PM

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