Thread: scanf in block problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    scanf in block problem

    Code:
    switch (choice)
    		{
    		case 1:
    		printf("Enter Letter:");	
    		scanf("%c", &letter);
    		printf("Its %c\n",letter);
    		break;
    		}
    why does the code ignore my scanf in a switch, loop, if etc statements. How can I get my code to stop and read user input.

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What happened before the switch?
    More scanf calls perhaps?

    scanf just consumes as many characters as it needs, so if you have
    scanf("%d", &myint);
    scanf("%c",&mychar);

    and you type in
    123\n
    Then myint is 123, and mychar contains '\n'

    If you type in
    123abc\n
    Then myint is 123, and mychar contains 'a', and the bc\n is left for someone else to deal with.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    No thats not the problem, sorry I did not explain it properly. When the code enters the switch it does not wait at all for user input therefore declaring NULL and goes on to display

    Enter Letter: Its

    I find it rather odd. It executes as a block of statements as it should do without waiting on any user input.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Salem explained the problem.

    How do you get choice?
    I'd guess...
    Code:
    scanf("%d", &choice);
    So you press 1 then <Enter>
    1 is consumed by your call to scanf and the newline character is left in the stdin buffer.

    Your second call to scanf then consumes the newline chracter as it's looking for a character (%c). It's done it's job at that point, read one character from stdin. It doesn't wait for more input since a character was already availible.

    print letter immediately after the scanf, you should see it prints a newline
    Last edited by spydoor; 01-09-2006 at 12:54 PM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Hi Spydoor what a bizarre way of doing it though but this code worked.

    Code:
    fflush(stdout);
    Then with this while loop to clear the buffer after the scanf

    Code:
    while((c = getchar()) != EOF && c != '\n') {
    	     ;
    	 }
    This worked but is their a better way of doing it, maybe an input that clears automatically!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, use fgets() to read all input into a buffer.
    Then use sscanf(), or some other conversion which works on data in memory, to extract the data which interests you.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Does fflush() clear the errors from the specified stream?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, it doesn't: http://www.cplusplus.com/ref/cstdio/fflush.html

    So you'd be better off using
    Code:
    clearerr(stdout);
    instead/as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf problem
    By kiranck007 in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 09:27 AM
  2. scanf problem
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-29-2005, 01:52 AM
  3. Problem: 64k block issue
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 06-16-2004, 03:22 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM