
LANGUAGE: C++
CHALLENGE:
Assume that c is a char variable has been declared. Write some code that reads in the first character of the next line into c. Assume that the lines of input are under 100 characters long.
SOLUTION:
cin.ignore(20,'\n'); cin.get(c);
Can someone explain what the
cin.ignore(20,’\n’);
does in this and why it’s put in?
It will get rid of the next 20 characters in the input stream, or everything up to and including the next new line character it comes it. That way you can just skip ahead to whatever ever is after that. It says read the first character of the NEXT line into the variable. So that moves ahead to the next line, bypasses everything between the start of the current line, up until the start of the next one, which is what you want to put into the variable c.
See here for more info (http://www.cplusplus.com/reference/istream/istream/ignore/?kw=cin.ignore)
why the name 20?
Why do you use 20?