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.
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.
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.*
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.
ohh ok so all it tells the program to do basicly is " Ok take this number but jsut ignore the enterbutton"?
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;}
>> 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.