C++ Question: ignore(100,'/n');

Sorry, I was imagining reading from a file. Frankly, as Cleophus noted, that cin>>salesamt; example is odd. I think a more “real world” example would use reading from a file. Something like the following, where file.dat is a file containing the data and comments from my last post.

(I’m wondering if you’ve gotten to reading from files yet? fin below is just like cin, except it’s reading from a file instead of the command line.)

  float a, b, c;
  char ch;
  fstream fin("file.dat",ios::in);
  while (!fin.eof()) {
    ch = fin.getc();
    if (ch == '#') {
    } else {
      fin >> a >> b >> c;
//   Additional processing...

  • I’m not sure if getc() is the right function for reading a single character. I never do that.

In the code snip, each time through the loop handles one line of the data file. The while(!fin.eof()) is just checking whether we’ve read to the end of the file. Only the three data lines are read into a, b, and c and processed.

Whenever a user types into standard input and presses the enter/return key, the special newline character is added to the end of the line, like so
User types -> hello
The line sent to your program in the standard input stream: "hello
", where
represents the newline character.

When cin is used, the newline remains in the input stream. If getline is used afterward, the function will read the newline and immediately terminate, placing only the newline in the variable given to getline. For getline to work as expected after using cin, the newline character must be ignored. cin.ignore() (note the empty parameter list) will do the job. It seems your textbook author wanted to explicitly specify a newline is to be ignored, and in order to do so both parameters must be specified, and 100 is probably a “magic”/BS value that was added to satisfy C++ syntax rules.

So, I’m basically trying to ‘ignore’ information that is sent as part of some sort of normal operation, rather than trying to ignore the data entered from the user?
Also, the 100 is giving room for the rest of the info that getline will be receiving?


cin left a newline on the stream. you should call ignore(1) to get rid of it. ignore(100,’
') is a uselessly roundabout way to do the same. If you don’t get rid of the newline, getline() will be tripped up.

If you’re so confused about a piece of code, why don’t you just type it up and see how it works? Comment out pieces of it, etc. This isn’t 1973. You don’t have to stand in line to be allocated 5 milliseconds on a mainframe. Figuring out how code works just through the power of thought can be a challenge even for a pro (in this case, very few pro programmers use cin and know such trivia about it), so props to Cleophus for figuring this out.

Sounds good! Thanks, everybody!!

And, definitely, Bravo, Cleophus!

One thing to remember that this function can be used with any iostream class; Instead of thinking it just for human input, consider it for a file. A file could have header which you want to ignore, or some other padding (if you reading a raw stream for TCP/IP for examle).