Remove '\n'

This is a discussion on Remove '\n' within the C Programming forums, part of the General Programming Boards category; Hi guys, I am not using fflush(stdin) anymore, after reading about it in the FAQ. I still have 1 question: ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    Remove '\n'

    Hi guys,

    I am not using fflush(stdin) anymore, after reading about it in the FAQ.
    I still have 1 question:

    Code:
    void FlushStdin(void)
    {
    	int iCh;
    	while ((iCh=getchar())!='\n' && iCh!=EOF);
    }
    
    int main(void)
    {
    	int a,b,c;
    	puts("Enter num 1");
    	scanf("%d",&a);
    	puts("Enter num 2");
    	scanf("%d",&b);
    	puts("Enter num 3");
    	scanf("%d",&c);
    
                    puts("Flushing 1");
    	FlushStdin();
    	puts("Flushing 2");
    	FlushStdin();
    	puts("Flushing 3");
    	FlushStdin();
    }
    My logic tells me that if I use scanf() 3 times, 1 after another, the buffer should contain 3 '\n' leftovers.
    Running the code proove I'm wrong, can someone please explain why there is only 1 newline in the buffer and not 3?

    Thank you!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    1 more thing, if the buffer return EOF when we arrived to the end of it, why is it that when there is no data in the buffer, the while loop runs until a key pressed.
    Why will the first attempt to read an empty buffer will not return EOF?

    Thanks again
    Last edited by salvadoravi; 01-11-2008 at 06:45 AM. Reason: mistake

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    It's because the first two scanf calls will read (and ignore) newlines. It's only the last scanf() call which will leave the newline present for you to count.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    [QUOTE=Salem;705497]It's because the first two scanf calls will read (and ignore) newlines. [QUOTE]
    Is that mean that the first two scanf calls will REMOVE the first 2 newlines from the buffer?
    Is there any situation where the buffer can contain more than 1 newline?

    Thank again

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    It could be any number (though usually 1).

    Think of the input as a stream of bytes, with occasional newlines. Newlines are important on a buffered stream because that's usually when the OS gives the line to your program.

    For most parts, scanf treats newlines as white space, so you could easily type in
    1 2 3\n
    or
    1\n
    2\n
    3\n

    into your program, and it would do exactly the same thing.

    Newlines are also important when you switch input methods (say scanf to fgets), where the trailing newlines left by scanf can really confuse later on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running remove() gets permission denied error...
    By edomingox in forum C Programming
    Replies: 4
    Last Post: 01-11-2009, 11:55 AM
  2. randomly remove elements
    By joan in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 12:46 PM
  3. randomly remove elements
    By joan in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2006, 11:22 AM
  4. Remove " character
    By Kirmie in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2005, 09:18 PM
  5. trying to remove item from an array
    By rugger78 in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2004, 10:26 AM

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