Thread: I never learned about cin.ignore()...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    I never learned about cin.ignore()...

    I never really did learn much of anything about buffers and cin.ignore() can anyone enlighten me as to how these work and all? It's my understanding that if you don't use endl to clear the buffer or something like that....a lot of stuff can remain there and be printed out in subsequent output statements?

    ....Something along those lines?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    It's my understanding that if you don't use endl to clear the buffer or something like that....a lot of stuff can remain there and be printed out in subsequent output statements?
    '\n' puts a newline at the end of the output, so you can do this:

    cout<<"some text\n";

    endl tacks on a '\n' to the output and calls flush() if the stream is buffered(is cout buffered?) It doesn't seem like cout is buffered:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout<<"bye";
    	return 0;
    }
    displays the text with my compiler. Maybe the compiler flushes the stream before ending the program.

    cin.ignore() is the same thing as cin.ignore(1) because 1 is the default value. It ignores one character in the input stream. ignore() actually has two parameters and the second one is the delimiter--ignore will skip the number of characters specified or until it hits the delimiter.
    Last edited by 7stud; 02-26-2005 at 05:27 PM.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    cout may or may not be buffered, cerr is not buffered. The buffer is flushed when the destructor is called(this applies to all streams) cout is also tieed to cin so calling cin >> i calls flush on cout. Thus it's hard to find a situation where you would notice that the buffer has not been flushed.
    The only time you might care is if your program dies unexpectedly
    Code:
        std::cout << "aproaching death";
        *(reinterpret_cast<int *>(0)) = 5;
    this may or may not print anything. Adding endl requres that the user will see the output before you do anything else. Most of the time it just wastes cycles.

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. Why do we use cin.ignore()
    By himanch in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2005, 01:52 PM
  3. Love - learned or inherent?
    By axon in forum A Brief History of Cprogramming.com
    Replies: 42
    Last Post: 01-24-2005, 12:09 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. What have you learned?
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-01-2002, 11:12 PM