Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By quzah

Confused with -> while(getchar() != '\n');

This is a discussion on Confused with -> while(getchar() != '\n'); within the C Programming forums, part of the General Programming Boards category; Hi I don't understand the code int c, j; while(scanf("%i", &j) != 1) while((c = getchar()) != '\n') ; I ...

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

    Talking Confused with -> while(getchar() != '\n');

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    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,193
    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.
    czar likes this.
    Right 98% of the time, and don't care about the other 3%.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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.
    AndrewHunter likes this.
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    "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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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. Why two getchar() ?
    By ekosix in forum C Programming
    Replies: 2
    Last Post: 06-05-2010, 01:13 PM
  2. getchar & cin.get
    By 74466 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2006, 04:29 AM
  3. getchar() and '\b'
    By snfiddy in forum C Programming
    Replies: 2
    Last Post: 11-30-2005, 04:52 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 08:25 PM

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