Why the program prints line twice?

This is a discussion on Why the program prints line twice? within the C Programming forums, part of the General Programming Boards category; details in the reply.....

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    9

    Why the program prints line twice?

    details in the reply..
    Last edited by red463; 04-29-2010 at 05:09 PM. Reason: nonconformity

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    Hi all!

    I cannot figure out why the output prints the printf line twice.. in this program.. thanks..

    here is the code with the output:

    Code:
    #include<stdio.h>
    main()
    {
    char ch;
    while (ch!='0')
    	{
    	printf("enter a char other than 0\n");
    	ch=getchar();
    	}
    }

    enter a char other than 0
    d
    enter a char other than 0
    enter a char other than 0

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What happens after you press the d key?

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

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    well, it prints "enter a char other than 0" twice...

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by red463 View Post
    Hi all!

    I cannot figure out why the output prints the printf line twice.. in this program.. thanks..

    here is the code with the output:

    Code:
    #include<stdio.h>
    main()
    {
    char ch;
    while (ch!='0')
    	{
    	printf("enter a char other than 0\n");
    	ch=getchar();
    	}
    }

    enter a char other than 0
    d
    enter a char other than 0
    enter a char other than 0
    Yes, but you are also probably pressing another button after 'd'.

    This is where I believe quzah is trying to lead you.

    Also, on another note, initialize your variables before use. -- although this is not the reason you are getting that behavior.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by red463 View Post
    well, it prints "enter a char other than 0" twice...
    No. I asked what happens after you press the D key. The answer was:

    "I press the enter key."

    Which should have given you the reason it is printing the line twice.


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

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    what is the solution to this problem?

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    ok teacher, what is the right way to deal with this then?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by red463 View Post
    ok teacher, what is the right way to deal with this then?
    The answer is either "recognize that this is the right answer" or "also check if the read character is enter-key". (EDIT TO ADD: For instance, think about what happens when they type "flibbertigibbet" at the prompt.)

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    Quote Originally Posted by tabstop View Post
    The answer is either "recognize that this is the right answer" or "also check if the read character is enter-key". (EDIT TO ADD: For instance, think about what happens when they type "flibbertigibbet" at the prompt.)
    when you do that, it would print the command n+1 times (n=number of chars in flibbertigibbet).
    I would please like to know how to deal with this, I am very new to programming, and cryptic answers aren't helping me here...

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by red463 View Post
    when you do that, it would print the command n+1 times (n=number of chars in flibbertigibbet).
    I would please like to know how to deal with this, I am very new to programming, and cryptic answers aren't helping me here...
    It really depends on what the specs of the program are. If the specs are something like "print a prompt and read a character; if the character is not '0', then repeat" then we will say it for the fourth time in this thread: there is no error in your code and therefore there is nothing to fix. If the specs are something other than that, then you need to tell us what they are.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    allright, my bad, the spec follows-

    write a program, which allows users to enter characters until the input is '0'.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Without using system specific functions (kbdhit, getch), you can't do what your program specs, as written in your last post, request. There's nothing keeping you from entering: "1230321" and hitting enter. So unless you read keystrokes - not characters - you can't do what you want.


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

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    well, thanks for all the help guys...

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your program fits the requirement as given by you, if you change the wording slightly. It will allow you to enter characters, and it will stop the loop when it hits '0'.

    All the rest has to do with buffering, and the way input is actually typed versus how it is being handled.


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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-18-2006, 02:31 AM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Write a program that prints a pattern of asterisks?
    By Basia in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 05:56 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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