why printf("...") statement is executing 2 times...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhu542
    New Member
    • Oct 2007
    • 17

    #1

    why printf("...") statement is executing 2 times...?

    Hi,

    I wrote a c-progam code , for to print whether the user has entered the digit or charater.

    The code is :-
    [code=c]
    int
    main(int argc, char* argv[])
    {
    int choice;
    char ch = '\0';

    printf("\n Enter your choice ... :");
    ch = getchar();

    do
    {
    printf("\nEnter your choice ... :");
    ch = getchar();

    if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <='Z') )
    {
    puts("\nHello.. . U Entered charater");
    }
    else if(isdigit(ch))
    {
    printf("\nThe digit U Entered is :%c", ch);
    }

    if(ch == '.')
    break;
    }while(1);
    return 0;

    }
    [/code]
    Problem:-

    for the first iteration it is working fine, then from the next iteration it is asking me 2 times the printf("Enter your choice..." ); statement.

    like this...

    Enter your choice ... :
    Enter your choice ... :

    Plz... help me how to solve this...

    Thanks & Regards...
    Last edited by sicarie; Oct 10 '07, 01:55 PM. Reason: Code tags
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by madhu542
    [code=c]
    int
    main(int argc, char* argv[])
    {
    int choice;
    char ch = '\0';

    printf("\n Enter your choice ... :");
    ch = getchar();

    do
    {
    printf("\nEnter your choice ... :");
    ch = getchar();

    if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <='Z') )
    {
    puts("\nHello.. . U Entered charater");
    }
    else if(isdigit(ch))
    {
    printf("\nThe digit U Entered is :%c", ch);
    }

    if(ch == '.')
    break;
    }while(1);
    return 0;

    }
    [/code]
    Programs start from the first line and work their way down. So starting at the line main(), the next commands you give to the computer are to create a integer called choice and a char called ch. Then you print to the console "Enter your choice...", the next thing after that tells the console to accept one char as input. Then you enter your do...while loop where you again print "Enter you choice..." and then again ask the console to accept one char as input. Now that I have explained what the computer is doing why do you think "Enter your choice..." appears twice? If you still can't figure it out, you should read up on do...while loops, programming in general, and perhaps find a debugger to step through the execution of your program.

    Comment

    • rijesh
      New Member
      • Mar 2007
      • 4

      #3
      Hi
      Its bcz u r using getchar(). After entering number or alphabet u wl press ente key.This wil take as char and the loop wil run twice. so "Enter your choice" wil be printed twice.plz execute d modified program given below. u wil understand better.


      spoonfeeding not allowed

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by rijesh
        Hi
        Its bcz u r using getchar(). After entering number or alphabet u wl press ente key.This wil take as char and the loop wil run twice. so "Enter your choice" wil be printed twice.plz execute d modified program given below. u wil understand better.


        spoonfeeding not allowed
        Actually the problem doesn't have anything to do with getchar(). Its just the way the program is written. On the first execution it asks the user to Enter your choice twice.

        Comment

        • niazdokrat
          New Member
          • Oct 2007
          • 10

          #5
          correct... getchar() is nto the problem since you still need to press enter to send the data fromt he input buffer to the function. the problem is he is plainly asking to enter teh char twice

          Comment

          • rijesh
            New Member
            • Mar 2007
            • 4

            #6
            Originally posted by RedSon
            Actually the problem doesn't have anything to do with getchar(). Its just the way the program is written. On the first execution it asks the user to Enter your choice twice.
            Its not only in d first time. Each loop it wil ask twice... if u want u can execute and see.. if it is not the problem with getchar(), tell me the reason

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Originally posted by rijesh
              Its not only in d first time. Each loop it wil ask twice... if u want u can execute and see.. if it is not the problem with getchar(), tell me the reason
              Given the code below once the program enters the loop it will only ask one time per loop. So your statement is incorrect.

              Comment

              • doskey
                New Member
                • Sep 2007
                • 10

                #8
                Easy....

                printf("\n Enter your choice ... :");
                ch = getchar();

                do
                {
                printf("\nEnter your choice ... :");

                Your telling the program to print it twice...

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by RedSon
                  Given the code below once the program enters the loop it will only ask one time per loop. So your statement is incorrect.
                  No it isn't. In this particular program the first time around the prompt is written
                  twice but also, you have to press "a<enter>" when you just want to press "a"
                  you have entered *two* characters. The <enter> key neither is a letter nor a digit
                  so nothing else is printed in the body of that loop.

                  The control flow of this working.

                  kind regards,

                  Jos

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    Originally posted by JosAH
                    No it isn't. In this particular program the first time around the prompt is written
                    twice but also, you have to press "a<enter>" when you just want to press "a"
                    you have entered *two* characters. The <enter> key neither is a letter nor a digit
                    so nothing else is printed in the body of that loop.

                    The control flow of this working.

                    kind regards,

                    Jos
                    Oh I see what you are saying, it appears to the output that its being written twice, which is true because there is a whitespace character, but it is only being written once per execution of the loop.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by RedSon
                      Oh I see what you are saying, it appears to the output that its being written twice, which is true because there is a whitespace character, but it is only being written once per execution of the loop.
                      Yep, that's it, and I can't even type properly after more than thirty years of trying.
                      I wanted to type "The control flow of this needs working." but of course I goofed
                      again ;-)

                      kind regards,

                      Jos

                      Comment

                      • madhu542
                        New Member
                        • Oct 2007
                        • 17

                        #12
                        Originally posted by JosAH
                        Yep, that's it, and I can't even type properly after more than thirty years of trying.
                        I wanted to type "The control flow of this needs working." but of course I goofed
                        again ;-)

                        kind regards,

                        Jos
                        Hai,

                        I mistakenly posted the code with the above printf(....) and getchar(); statements those should be commented...
                        without those statements also I am getting printf("---") statement twice.

                        Thanks & Regards...

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by madhu542
                          Hai,

                          I mistakenly posted the code with the above printf(....) and getchar(); statements those should be commented...
                          without those statements also I am getting printf("---") statement twice.

                          Thanks & Regards...
                          My reasoning still applies here: when you want to type, say, 'a' you actually
                          type 'a<enter>' that is the 'a' character and the '\n' character. The second time
                          in the body of the loop that '\n' character is read which is neither a letter nor
                          a digit, and nothing is printed. The next time the loop of the body is read the
                          input buffer is empty and your program waits again until you type another one.

                          For a little experiment type 'foo<enter>' and see what happens (that is four
                          characters: 1: f, 2: o, 3: o and 4: <enter>),

                          kind regards,

                          Jos

                          Comment

                          • madhu542
                            New Member
                            • Oct 2007
                            • 17

                            #14
                            Originally posted by JosAH
                            My reasoning still applies here: when you want to type, say, 'a' you actually
                            type 'a<enter>' that is the 'a' character and the '\n' character. The second time
                            in the body of the loop that '\n' character is read which is neither a letter nor
                            a digit, and nothing is printed. The next time the loop of the body is read the
                            input buffer is empty and your program waits again until you type another one.

                            For a little experiment type 'foo<enter>' and see what happens (that is four
                            characters: 1: f, 2: o, 3: o and 4: <enter>),

                            kind regards,

                            Jos
                            yes it is printing printf statement 4 times...
                            but how to solve this...problem?

                            Thanks & Regards

                            Comment

                            • poonguzhaly
                              New Member
                              • Oct 2007
                              • 1

                              #15
                              Originally posted by madhu542
                              yes it is printing printf statement 4 times...
                              but how to solve this...problem?

                              Thanks & Regards

                              If the input stream is reading a white space character, get the input through scanf("%1s", &c);

                              This 1s will omit the white space character...

                              Thanks
                              Poonguzhaly

                              Comment

                              Working...