take input char from keyboard

Discussion in 'C Programming' started by j.smith2c@googlemail.com, Dec 5, 2007.

  1. Guest

    Hi everyone,

    I wanted to run the following loop,
    and enter y everytime (I expected that it will ask
    me to enter y for 5 time), but just after 3 times program terminates.
    what is the error??


    #include <stdio.h>

    int main ()
    {
    char ch;
    int j=0;
    for (j ; j<5 ; j++)
    {
    printf("Please enter the input character :\n\n");
    scanf("%c", &ch);
    printf("You have entered : %c \n\n" , ch);

    if (ch == 'y' || ch == 'Y')
    printf("HIIIIIIIII \n");
    }
    return 0;
    }
    , Dec 5, 2007
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hi everyone,
    >
    > I wanted to run the following loop,
    > and enter y everytime (I expected that it will ask
    > me to enter y for 5 time), but just after 3 times program terminates.
    > what is the error??
    >
    >
    > #include <stdio.h>
    >
    > int main ()
    > {
    > char ch;
    > int j=0;
    > for (j ; j<5 ; j++)
    > {
    > printf("Please enter the input character :\n\n");
    > scanf("%c", &ch);
    > printf("You have entered : %c \n\n" , ch);


    Change the above line to:

    printf("You have entered: %c (decimal %d) \n", ch, (int)ch);

    and run it again. Look carefully at the results.

    >
    > if (ch == 'y' || ch == 'Y')
    > printf("HIIIIIIIII \n");
    > }
    > return 0;
    > }


    -Mike
    Mike Wahler, Dec 5, 2007
    #2
    1. Advertising

  3. Guest

    Now it gives following output, but it is not the expected output.
    I still could not find what the problem is.

    Please enter the input character :

    y
    You have entered: y (decimal 121)
    HIIIIIIIII
    Please enter the input character :

    You have entered:
    (decimal 10)
    Please enter the input character :

    y
    You have entered: y (decimal 121)
    HIIIIIIIII
    Please enter the input character :

    You have entered:
    (decimal 10)
    Please enter the input character :

    y
    You have entered: y (decimal 121)
    HIIIIIIIII




    On Dec 5, 1:29 am, "Mike Wahler" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    > > Hi everyone,

    >
    > > I wanted to run the following loop,
    > > and enter y everytime (I expected that it will ask
    > > me to enter y for 5 time), but just after 3 times program terminates.
    > > what is the error??

    >
    > > #include <stdio.h>

    >
    > > int main ()
    > > {
    > > char ch;
    > > int j=0;
    > > for (j ; j<5 ; j++)
    > > {
    > > printf("Please enter the input character :\n\n");
    > > scanf("%c", &ch);
    > > printf("You have entered : %c \n\n" , ch);

    >
    > Change the above line to:
    >
    > printf("You have entered: %c (decimal %d) \n", ch, (int)ch);
    >
    > and run it again. Look carefully at the results.
    >
    >
    >
    > > if (ch == 'y' || ch == 'Y')
    > > printf("HIIIIIIIII \n");
    > > }
    > > return 0;
    > > }

    >
    > -Mike
    , Dec 5, 2007
    #3
  4. "" <> writes:

    > Now it gives following output, but it is not the expected output.
    > I still could not find what the problem is.


    This is question 12.18b of the C FAQ:

    http://c-faq.com/stdio/scanfc.html

    > Please enter the input character :
    >
    > y
    > You have entered: y (decimal 121)
    > HIIIIIIIII
    > Please enter the input character :
    >
    > You have entered:
    > (decimal 10)
    > Please enter the input character :

    <snip similar>

    PS. Please snip your replies to include on the parts you are
    commenting on.

    --
    Ben.
    Ben Bacarisse, Dec 5, 2007
    #4
  5. Guest

    On Dec 5, 2:00 am, Ben Bacarisse <> wrote:
    > "" <> writes:
    > > Now it gives following output, but it is not the expected output.
    > > I still could not find what the problem is.

    >
    > This is question 12.18b of the C FAQ:
    >
    > http://c-faq.com/stdio/scanfc.html
    >
    > > Please enter the input character :

    >
    > > y
    > > You have entered: y (decimal 121)
    > > HIIIIIIIII
    > > Please enter the input character :

    >
    > > You have entered:
    > > (decimal 10)
    > > Please enter the input character :

    >
    > <snip similar>
    >
    > PS. Please snip your replies to include on the parts you are
    > commenting on.
    >
    > --
    > Ben.


    Hello all,

    Still I could not solve the problem mentioned.
    Do anyone can tell "if I want to run for loop from 1 to 5, everytime
    it ask user to enter a character , and if that
    character is same with y, then print "Hello" ".

    Thank you.

    smith
    , Dec 5, 2007
    #5
  6. Mark Bluemel Guest

    wrote:

    What part of

    >> PS. Please snip your replies to include on the parts you are
    >> commenting on.


    didn't you understand?

    > Still I could not solve the problem mentioned.


    Then you didn't read the FAQ carefully enough.

    > Do anyone can tell "if I want to run for loop from 1 to 5, everytime
    > it ask user to enter a character , and if that
    > character is same with y, then print "Hello" ".


    That's a trivial task, but made much harder by your decision
    to use scanf().

    Try reading the whole of http://c-faq.com/stdio/index.html - question
    12.20 is especially relevant.

    Then consider rewriting your code without scanf().
    Mark Bluemel, Dec 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    546
  2. wwj
    Replies:
    24
    Views:
    2,499
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    469
    Tristan Miller
    Jan 17, 2004
  4. Replies:
    4
    Views:
    664
    Walter Roberson
    Sep 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,034
    Ian Collins
    May 9, 2006
Loading...

Share This Page