getchar can't be used twice?

Discussion in 'C Programming' started by Jonathan, Apr 13, 2006.

  1. Jonathan

    Jonathan Guest

    Hi--

    I have the following code:


    #include <stdio.h>

    char a,b;

    int main()
    {

    printf("Which character is greater?\n");
    printf("Type a single character:");
    a=getchar();
    printf("Type another character:");
    b=getchar();

    printf("The first character is %c. The second character is %c.\n", a,
    b);

    return 0;
    }


    But when run, it takes only the input for the first character, then
    continues without pausing for the next input:


    [Session started at 2006-04-13 13:31:15 -0700.]
    Which character is greater?
    Type a single character:5
    Type another character:The first character is 5. The second character is
    ..


    What am I doing wrong?

    Best,
    Jonathan
    Jonathan, Apr 13, 2006
    #1
    1. Advertising

  2. Jonathan

    Bill Pursell Guest

    Jonathan wrote:

    > I have the following code:
    >
    > #include <stdio.h>
    > char a,b;
    > int main()
    > {
    > printf("Which character is greater?\n");
    > printf("Type a single character:");
    > a=getchar();
    > printf("Type another character:");
    > b=getchar();
    >
    > printf("The first character is %c. The second character is %c.\n", a,
    > b);
    >
    > return 0;
    > }
    >
    >
    > But when run, it takes only the input for the first character, then
    > continues without pausing for the next input:
    >


    If the user types '5' and hits return, the first getchar() consumes the
    '5', and the 2nd consumes the '\n'. A simple solution is to put an
    extra getchar() right after the a=getchar() line to consume the '\n'.
    Bill Pursell, Apr 13, 2006
    #2
    1. Advertising

  3. Jonathan <> writes:
    > I have the following code:
    >
    >
    > #include <stdio.h>
    >
    > char a,b;
    >
    > int main()
    > {
    >
    > printf("Which character is greater?\n");
    > printf("Type a single character:");
    > a=getchar();
    > printf("Type another character:");
    > b=getchar();
    >
    > printf("The first character is %c. The second character is %c.\n", a,
    > b);
    >
    > return 0;
    > }


    getchar() returns int, not char, so it can distinguish EOF from any
    valud input character value. It doesn't matter much for this simple
    program, but you should get into the habit of using it correctly.

    > But when run, it takes only the input for the first character, then
    > continues without pausing for the next input:
    >
    >
    > [Session started at 2006-04-13 13:31:15 -0700.]
    > Which character is greater?
    > Type a single character:5
    > Type another character:The first character is 5. The second character is
    > .
    >
    >
    > What am I doing wrong?


    In response to the first prompt, you typed two characters, not one:
    the '5' and a new-line. You assigned the value '5' to a, and the
    value '\n' to b.

    Try running the program again, typing "56" followed by a new-line;
    you'll get '5' in a and '6' in b.

    Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 13, 2006
    #3
  4. Jonathan

    Jonathan Guest

    in article , Keith Thompson at
    wrote on 4/13/06 1:52 PM:

    > Jonathan <> writes:
    >> I have the following code:
    >>
    >>
    >> #include <stdio.h>
    >>
    >> char a,b;
    >>
    >> int main()
    >> {
    >>
    >> printf("Which character is greater?\n");
    >> printf("Type a single character:");
    >> a=getchar();
    >> printf("Type another character:");
    >> b=getchar();
    >>
    >> printf("The first character is %c. The second character is %c.\n", a,
    >> b);
    >>
    >> return 0;
    >> }

    >
    > getchar() returns int, not char, so it can distinguish EOF from any
    > valud input character value. It doesn't matter much for this simple
    > program, but you should get into the habit of using it correctly.
    >
    >> But when run, it takes only the input for the first character, then
    >> continues without pausing for the next input:
    >>
    >>
    >> [Session started at 2006-04-13 13:31:15 -0700.]
    >> Which character is greater?
    >> Type a single character:5
    >> Type another character:The first character is 5. The second character is
    >> .
    >>
    >>
    >> What am I doing wrong?

    >
    > In response to the first prompt, you typed two characters, not one:
    > the '5' and a new-line. You assigned the value '5' to a, and the
    > value '\n' to b.
    >
    > Try running the program again, typing "56" followed by a new-line;
    > you'll get '5' in a and '6' in b.
    >
    > Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.



    Yike. I tried reading that section and still don't understand this. If it's
    the case that getchar is getting the \n as an input, wouldn't that mean that
    "a" would be assigned to \n and "b" to 5, not the other way around?

    In other words, why is it taking the \n after the a var is input? I don't
    see where it's getting that from.

    Best,
    Jonathan
    Jonathan, Apr 13, 2006
    #4
  5. Jonathan <> writes:
    > in article , Keith Thompson at
    > wrote on 4/13/06 1:52 PM:
    >> Jonathan <> writes:
    >>> I have the following code:
    >>>
    >>>
    >>> #include <stdio.h>
    >>>
    >>> char a,b;
    >>>
    >>> int main()
    >>> {
    >>>
    >>> printf("Which character is greater?\n");
    >>> printf("Type a single character:");
    >>> a=getchar();
    >>> printf("Type another character:");
    >>> b=getchar();
    >>>
    >>> printf("The first character is %c. The second character is %c.\n", a,
    >>> b);
    >>>
    >>> return 0;
    >>> }

    >>
    >> getchar() returns int, not char, so it can distinguish EOF from any
    >> valud input character value. It doesn't matter much for this simple
    >> program, but you should get into the habit of using it correctly.
    >>
    >>> But when run, it takes only the input for the first character, then
    >>> continues without pausing for the next input:
    >>>
    >>>
    >>> [Session started at 2006-04-13 13:31:15 -0700.]
    >>> Which character is greater?
    >>> Type a single character:5
    >>> Type another character:The first character is 5. The second character is
    >>> .
    >>>
    >>>
    >>> What am I doing wrong?

    >>
    >> In response to the first prompt, you typed two characters, not one:
    >> the '5' and a new-line. You assigned the value '5' to a, and the
    >> value '\n' to b.
    >>
    >> Try running the program again, typing "56" followed by a new-line;
    >> you'll get '5' in a and '6' in b.
    >>
    >> Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    >
    >
    > Yike. I tried reading that section and still don't understand this. If it's
    > the case that getchar is getting the \n as an input, wouldn't that mean that
    > "a" would be assigned to \n and "b" to 5, not the other way around?
    >
    > In other words, why is it taking the \n after the a var is input? I don't
    > see where it's getting that from.


    They're just assigned in the order in which you entered them.

    When you ran the program, you entered two characters, '5' and a
    new-line. Your program called getchar() twice. The first call
    returned the first character you entered, '5'; the second returned the
    second character you entered, '\n'.

    The confusing thing is that input is line-buffered. getchar() doesn't
    (normally) read directly from the keyboard; instead, it works on top
    of some implementation-specific lower level code. The low level code
    reads an entire line of input, storing the '5' and '\n' characters
    together in an internal buffer. getchar() grabs successive characters
    from that buffer, waiting for more input when the buffer is empty.

    If it weren't for this input buffering (and there are ways to turn it
    off on some systems), typing the '5' would cause getchar() to return
    the value '5' immediately, and you'd see the second prompt *before*
    you hit <enter>. The prompts would be interleaved with your input
    differently, but you'd still get the same results for the same input;
    typing "5<ENTER>" would still store '5' in a and '\n' in b.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 13, 2006
    #5
  6. Jonathan

    Flash Gordon Guest

    Jonathan wrote:
    >
    >
    > in article , Keith Thompson at
    > wrote on 4/13/06 1:52 PM:
    >
    >> Jonathan <> writes:
    >>> I have the following code:
    >>>
    >>>
    >>> #include <stdio.h>
    >>>
    >>> char a,b;
    >>>
    >>> int main()
    >>> {
    >>>
    >>> printf("Which character is greater?\n");
    >>> printf("Type a single character:");
    >>> a=getchar();
    >>> printf("Type another character:");
    >>> b=getchar();
    >>>
    >>> printf("The first character is %c. The second character is %c.\n", a,
    >>> b);
    >>>
    >>> return 0;
    >>> }

    >> getchar() returns int, not char, so it can distinguish EOF from any
    >> valud input character value. It doesn't matter much for this simple
    >> program, but you should get into the habit of using it correctly.
    >>
    >>> But when run, it takes only the input for the first character, then
    >>> continues without pausing for the next input:
    >>>
    >>>
    >>> [Session started at 2006-04-13 13:31:15 -0700.]
    >>> Which character is greater?
    >>> Type a single character:5
    >>> Type another character:The first character is 5. The second character is
    >>> .
    >>>
    >>>
    >>> What am I doing wrong?

    >> In response to the first prompt, you typed two characters, not one:
    >> the '5' and a new-line. You assigned the value '5' to a, and the
    >> value '\n' to b.
    >>
    >> Try running the program again, typing "56" followed by a new-line;
    >> you'll get '5' in a and '6' in b.
    >>
    >> Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    >
    >
    > Yike. I tried reading that section and still don't understand this. If it's
    > the case that getchar is getting the \n as an input, wouldn't that mean that
    > "a" would be assigned to \n and "b" to 5, not the other way around?
    >
    > In other words, why is it taking the \n after the a var is input? I don't
    > see where it's getting that from.


    What order are you pressing keys in? Do you press return first then 5 or
    5 first then return? Do you think getchar should return characters in
    the order you enter them or in a different order?
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
    Flash Gordon, Apr 13, 2006
    #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. G Patel
    Replies:
    44
    Views:
    2,301
    Fao, Sean
    Feb 15, 2005
  2. Kiuhnm

    twice(twice(x))

    Kiuhnm, Apr 1, 2006, in forum: C++
    Replies:
    2
    Views:
    393
    Kiuhnm
    Apr 1, 2006
  3. Casey Hawthorne
    Replies:
    1
    Views:
    711
    Arne Vajhøj
    Mar 18, 2009
  4. Ravikanth
    Replies:
    2
    Views:
    612
    Ravikanth
    Jul 7, 2011
  5. Ted Byers
    Replies:
    23
    Views:
    419
    Peter J. Holzer
    Nov 15, 2008
Loading...

Share This Page