why the second scanf function cannot run when i put inside the loop?

This is a discussion on why the second scanf function cannot run when i put inside the loop? within the C Programming forums, part of the General Programming Boards category; Hi, i am the beginner for c programming, hope someone can help me. Why the second scanf function cannot run ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    why the second scanf function cannot run when i put inside the loop?

    Hi, i am the beginner for c programming, hope someone can help me.

    Why the second scanf function cannot run when i put the code inside the loop?
    But the second scanf function will become normal and can run if without the loop.
    The c compiler i use is gcc compiler.

    here is the code
    Thanks for anyone can help me.



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    void main()
    {
             char         chrResponse='N';
             int            intA                    ;
    
             do
             {
                          printf("Please enter a number here  !!");
                          scanf("%d", &intA);
                         
                          printf("Do you want to continue ? (Y/N);
                          fflush(stdin);
                          scanf("%c", &chrResponse);
              }while(chrResponse=='Y'||chrResponse=='y');
    
    
              return;
    
    }
    I use the variable chrResponse to control the loop, when i key in Y or y it will continue the program.

    I initialize the chrResponse to 'n'. The problem is i can key in the first data for variable intA, but after that the program directly print the message"Do you want to continue" and terminate it, it dind't allow me to enter a value for chrRespond either i want to continue the program or not.

    I hope some one can help let me know why it happpen.

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Flushing the input stream is undefined behavior. You're better off chomping the newline after your first call. Consider consulting the FAQ section of the site.


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

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Unlike using scanf with %d and other format specifiers, the %c specifier does not skip leading white-space. This means that the newline left in the input buffer from the first scanf call is still there and gets read by the second. One of the easiest ways to deal with this might be to just put a space in front of the %c specifier:
    Code:
    scanf(" %c",&chrResponse);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I guess that this issue has been raised like million times so far and everytime we get them, we start advising to clear the input buffer. lol

    Could there be any better way to reach this information to the beginners, apart from FAQ?

    -ssharish
    Last edited by ssharish2005; 06-23-2009 at 01:17 PM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Thanks for your help

    Thanks for l help.

    I will log into the FAQ section to have a look because i don't know this section before this, actually i am still not so clear about the scanf function but i i will post this question in FAQ section.

    I am appreciate for those give useful information to me, thank you.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Get rid of this:
    Code:
    fflush(stdin);
    scanf("%c", &chrResponse);
    and use this instead:
    Code:
    scanf(" %c", &chrResponse);  //note the extra space before the % sign

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. Having only one function run at higher priority
    By ulillillia in forum C Programming
    Replies: 35
    Last Post: 02-24-2008, 05:56 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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