Thread: Confused with -> while(getchar() != newline);

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    Talking Confused with -> while(getchar() != newline);

    Hi
    I don't understand the code

    int c, j;
    while(scanf("%i", &j) != 1)
    while((c = getchar()) != '\n')
    ;


    I read alot of topics and comments about getchar() function and i knew that
    getchar() is just a function gets the first character of the line the user entered

    Here in our example i understood that the code locks the console screen until the user
    hit the enter key ...but when i run the program
    ->in case i typed 1 the console freezes
    and in case i typed anything but 1 the console freezes too ?!

    Why ..
    could you please explain ..
    i'm new with c so be simple as you can ..
    thanks

  2. #2
    Registered User czar's Avatar
    Join Date
    Aug 2011
    Posts
    4
    scanf(), fscanf()
    %i is just like %d and scanf is returning the number of values assigned to variables
    the second while statement is executed until '\n' is encountered. This is the newline character. Or Enter

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    I understand the scanf() function
    and i know that the '\n' means new line but i don't understand the while loop code in total here

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can't reproduce your problem with the code that you posted.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    don't care about the whole code
    i'm just asking about getchar() function in the while loop


    while((c = getchar()) != '\n')
    ;


    what does this specific code mean?
    thanks

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm just concerned because you wrote this:
    Here in our example i understood that the code locks the console screen until the user
    hit the enter key ...but when i run the program
    ->in case i typed 1 the console freezes
    and in case i typed anything but 1 the console freezes too ?
    which told me that you thought the code was doing something wrong.

    Yeah I can explain what that line does. It calls getchar() until it returns a newline; when the newline is read, the loop will break. It is the operative code in your explanation here:
    i understood that the code locks the console screen until the user
    hit the enter key
    I would say that the console is "waiting for input" but whatever floats your boat.


    I'm really confused as to how you could write that explanation and ask about it, but I hope that you're clear.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salahuddin View Post
    don't care about the whole code
    i'm just asking about getchar() function in the while loop


    while((c = getchar()) != '\n')
    ;


    what does this specific code mean?
    In terms of mechanics, it keeps calling getchar() and storing the result in c, until the result is a '\n'.

    In terms of net effect, it gobbles characters from standard input until a newline ('\n') is encountered, and leaves a '\n' character in c.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    In terms of mechanics, it keeps calling getchar() and storing the result in c, until the result is a '\n'.

    In terms of net effect, it gobbles characters from standard input until a newline ('\n') is encountered, and leaves a '\n' character in c.
    It can also be simplified to this...
    Code:
    while (getchar() != '\n');
    Just a way of finding the end of the current line.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    But if you're only testing for \n, then the whole thing will lock up at EOF

    while ( (c = getchar()) != EOF && c != '\n' );
    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.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salahuddin View Post
    Hi
    I don't understand the code

    int c, j;
    while(scanf("%i", &j) != 1)
    while((c = getchar()) != '\n')
    ;
    You confused color with code. I occasionally do that myself (but more often than not, I confuse quote with code), but that's why we have an edit box.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Thanks for all of you guys
    I'm really grateful for your interesting

    Let me tell you that i'm new with C so let me say what i understood from all your answers

    Code:
    int main(void)
    {
    	char c ;
    	while((c = getchar()) != '\n');
    	printf("Enter a number");
    	return 0;
    }
    **It calls getchar() until it returns a newline

    ** when the newline is read, the loop will break

    **that means when the user hit the enter key and a newline is read the loop will break and the
    "Enter a number" will be printed on the screen .

    **Then the user can type any thing on the console ... right ?

    I'm not sure because when the "Enter a number " appears on my console the console freezes and i can't type any thing .. why?

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    "Enter a number" will be printed on the screen .

    **Then the user can type any thing on the console ... right ?
    The program..as it is now.. just exists after asking for the input.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salahuddin View Post
    **Then the user can type any thing on the console ... right ?
    Sure, after your program ends, since the last thing it does is print "Enter a number".

    Your console did "freeze" because of this program. What OS are you using?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    I'm working on ubuntu 11.04

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using getchar() to flush newline from input buffer
    By Valour549 in forum C Programming
    Replies: 15
    Last Post: 11-03-2018, 07:49 AM
  2. Confused about getchar and looping
    By andy_d in forum C Programming
    Replies: 8
    Last Post: 04-30-2014, 06:10 PM
  3. newline
    By st00ch in forum C Programming
    Replies: 6
    Last Post: 03-19-2011, 04:10 AM
  4. getting rid of newline?
    By KBriggs in forum C Programming
    Replies: 10
    Last Post: 05-07-2010, 12:30 PM
  5. I/O newline
    By Unlimited4s in forum C++ Programming
    Replies: 3
    Last Post: 08-03-2002, 11:45 AM