printf statement executed twice

This is a discussion on printf statement executed twice within the C Programming forums, part of the General Programming Boards category; Hi all, I wrote a program to run a function based on the users choice. Following is the code snippet ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    printf statement executed twice

    Hi all,
    I wrote a program to run a function based on the users choice.
    Following is the code snippet of the program.


    Code:
    int main()
    {
    
            char choice;
    
            while(1)
            {
    
                    printf("Enter your choice(y/n):");
                    scanf("%c",&choice);
    
                    if ( choice == 'y' )
                    {
    
                            create_node();
    
                    }
    
                    else if ( choice == 'n' )
                    {
    
                            break;
    
                    }
    
            }
    
            display();
    
    }
    In the code, the statement highlighted in blue color is getting printed twice except for the first time, when the while loop is getting executed.The output is shown in the following quote.

    Enter your choice(y/n):y
    Enter the data to be inserted:10
    Enter your choice(y/n):Enter your choice(y/n):1
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:1
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:9
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:2
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:8
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:3
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:7
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:4
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:6
    Enter your choice(y/n):Enter your choice(y/n):y
    Enter the data to be inserted:5
    Enter your choice(y/n):Enter your choice(y/n):n
    Did any one face this type of problem before.
    Can any one help in solving the problem?.

    Thanks in advance,
    anand.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    From your output it looks like it's getting printed twice once...when you enter something that is not handled by your if block, and the loop continues to the top. Ad a final else to break out of the loop if its not y or n. Or to print an error inbetween.

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    504
    That's because you enter not one but two characters(the character you typed plus the carriage return newline character) for "choice". As no condition is defined for '\n' it loops all the way back printing the string one more time.
    Add a
    Code:
    getchar();
    after scanf statement to eat the '\n' character. I don't know if this is an elegant way though..
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    scanf(" %c",&choice);
    You can add a space before the %c in the scanf to eat that newline.
    "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

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    Thanks a lot for your reponses!!!
    Every suggestion works Fine.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. Unreachable code?
    By Leojeen in forum C Programming
    Replies: 15
    Last Post: 09-28-2008, 08:11 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 10:25 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

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