new line character : scanf and getchar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mars

    new line character : scanf and getchar

    char c;
    int i;

    scanf("%d",&i);
    c=getchar();

    I want to read a integer and a character in 2 lines.
    Why the getchar always get the \n character from the last line?????

    (Sorry for my newbie question, but where is the FAQ section of the
    newsgroup??)
  • j

    #2
    Re: new line character : scanf and getchar


    "Mars" <Mars@Mars> wrote in message news:41fe3b7b$1 _3@rain.i-cable.com...[color=blue]
    > char c;
    > int i;
    >
    > scanf("%d",&i);
    > c=getchar();
    >
    > I want to read a integer and a character in 2 lines.
    > Why the getchar always get the \n character from the last line?????
    >[/color]

    In your case, stdin refers to an interactive device and as such, is line
    buffered.
    This means that data is transferred when a newline is encountered.

    If scanf's conversion succeeds you will have a newline left in your input
    stream.
    Any subsequent read issued on stdin will encounter this newline if you did
    not enter additional data after some integer that corresponds to your
    conversion
    specifier.

    One solution would be,

    void consume_stdin(v oid)
    {
    while((getchar( )) != '\n')
    ;
    }


    You should also do some error checking with respect to scanf's return value.
    [color=blue]
    > (Sorry for my newbie question, but where is the FAQ section of the
    > newsgroup??)[/color]





    --
    j


    Comment

    • Sontu

      #3
      Re: new line character : scanf and getchar

      Why don't you just write

      int i;
      char c;

      scanf("%d",&i);
      fflush(stdin); //flushes the input stream and gets rid of '\n'
      character
      c=getchar();

      Try it out, it will work.

      Comment

      • j

        #4
        Re: new line character : scanf and getchar


        "Sontu" <abhaywit@gmail .com> wrote in message
        news:1107189949 .518576.298550@ c13g2000cwb.goo glegroups.com.. .[color=blue]
        > Why don't you just write
        >
        > int i;
        > char c;
        >
        > scanf("%d",&i);
        > fflush(stdin); //flushes the input stream and gets rid of '\n'
        > character
        > c=getchar();
        >
        > Try it out, it will work.
        >[/color]

        Supposing that there is data in the input stream, where do you propose
        that it will be flushed to?

        This is actually addressed in the FAQ but I want you to answer my question.



        --
        j


        Comment

        • Richard Bos

          #5
          Re: new line character : scanf and getchar

          "Sontu" <abhaywit@gmail .com> wrote:
          [color=blue]
          > Why don't you just write[/color]

          Why doesn't _who_ just write that? Leave in some context when you post!
          If you must use Google-broken-beta, learn to use it properly.
          [color=blue]
          > int i;
          > char c;
          >
          > scanf("%d",&i);
          > fflush(stdin); //flushes the input stream and gets rid of '\n'[/color]

          Because the behaviour fflush() is undefined for input streams. It only
          works for output streams.
          [color=blue]
          > Try it out, it will work.[/color]

          Says you. On your edition of your favourite compiler, it may even do
          something. It's not required to do anything sane or safe, though.

          Richard

          Comment

          • CBFalconer

            #6
            Re: new line character : scanf and getchar

            Sontu wrote:[color=blue]
            >
            > Why don't you just write
            >
            > int i;
            > char c;
            >
            > scanf("%d",&i);
            > fflush(stdin); //flushes the input stream and gets rid of '\n'
            > character
            > c=getchar();
            >
            > Try it out, it will work.[/color]

            No it won't (portably). It may assasinate somebody. Please don't
            give incorrect advice around here. You should have looked up the
            validity of fflush for input devices (which is none).

            --
            "If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers." - Keith Thompson


            Comment

            • Keith Thompson

              #7
              Re: new line character : scanf and getchar

              Mars <Mars@Mars> writes:[color=blue]
              > char c;
              > int i;
              >
              > scanf("%d",&i);
              > c=getchar();
              >
              > I want to read a integer and a character in 2 lines.
              > Why the getchar always get the \n character from the last line?????[/color]

              The getchar() reads the '\n' character because the scanf() didn't read it.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

              Comment

              • Old Wolf

                #8
                Re: new line character : scanf and getchar

                j wrote:[color=blue]
                > "Mars" <Mars@Mars> wrote:[color=green]
                > > char c;
                > > int i;
                > >
                > > scanf("%d",&i);
                > > c=getchar();
                > >
                > > I want to read a integer and a character in 2 lines.
                > > Why the getchar always get the \n character from the last line?????
                > >[/color]
                >
                > In your case, stdin refers to an interactive device and as
                > such, is line buffered. This means that data is transferred
                > when a newline is encountered.[/color]

                Irrelevent, the same problem occurs on any stream. All you can
                deduce from this is that there was a newline character after
                the number.
                Exactly the same thing would have happened if stdin was coming
                from a non-interactive device (eg. a file), so I don't know
                how you can say that it did refer to an interactive device.
                [color=blue]
                > If scanf's conversion succeeds you will have a newline left in
                > your input stream.
                > Any subsequent read issued on stdin will encounter this newline
                > if you did not enter additional data after some integer that
                > corresponds to your conversion specifier.[/color]

                Subsequent reading will always encounter the newline (eventually).
                (I think this is what you intended to say, although on the first
                reading, it seemed to me that you were saying that if additional
                data was entered then the newline would not be encountered).

                Comment

                • j

                  #9
                  Re: new line character : scanf and getchar


                  "Old Wolf" <oldwolf@inspir e.net.nz> wrote in message
                  news:1107212041 .866295.204860@ f14g2000cwb.goo glegroups.com.. .[color=blue]
                  > j wrote:[color=green]
                  > > "Mars" <Mars@Mars> wrote:[color=darkred]
                  > > > char c;
                  > > > int i;
                  > > >
                  > > > scanf("%d",&i);
                  > > > c=getchar();
                  > > >
                  > > > I want to read a integer and a character in 2 lines.
                  > > > Why the getchar always get the \n character from the last line?????
                  > > >[/color]
                  > >
                  > > In your case, stdin refers to an interactive device and as
                  > > such, is line buffered. This means that data is transferred
                  > > when a newline is encountered.[/color]
                  >
                  > Irrelevent, the same problem occurs on any stream. All you can
                  > deduce from this is that there was a newline character after
                  > the number.
                  > Exactly the same thing would have happened if stdin was coming
                  > from a non-interactive device (eg. a file), so I don't know
                  > how you can say that it did refer to an interactive device.
                  >[/color]

                  Yeah, I'm not sure what I was thinking there.
                  [color=blue][color=green]
                  > > If scanf's conversion succeeds you will have a newline left in
                  > > your input stream.
                  > > Any subsequent read issued on stdin will encounter this newline
                  > > if you did not enter additional data after some integer that
                  > > corresponds to your conversion specifier.[/color]
                  >
                  > Subsequent reading will always encounter the newline (eventually).
                  > (I think this is what you intended to say, although on the first
                  > reading, it seemed to me that you were saying that if additional
                  > data was entered then the newline would not be encountered).
                  >[/color]

                  I was thinking in terms of what he presented.
                  He just included one call to getchar after scanf.

                  Something such as ``10aaaaaa\n''
                  would have yielded a valid conversion
                  with a subsequent read producing ``a''.



                  --
                  j


                  Comment

                  • T O

                    #10
                    Re: new line character : scanf and getchar



                    Old Wolf wrote:[color=blue]
                    > j wrote:
                    >[color=green]
                    >>"Mars" <Mars@Mars> wrote:
                    >>[color=darkred]
                    >>>char c;
                    >>>int i;
                    >>>
                    >>>scanf("%d",& i);
                    >>>c=getchar( );
                    >>>
                    >>>I want to read a integer and a character in 2 lines.
                    >>>Why the getchar always get the \n character from the last line?????
                    >>>[/color]
                    >>
                    >>In your case, stdin refers to an interactive device and as
                    >>such, is line buffered. This means that data is transferred
                    >>when a newline is encountered.[/color]
                    >
                    >
                    > Irrelevent, the same problem occurs on any stream. All you can
                    > deduce from this is that there was a newline character after
                    > the number.
                    > Exactly the same thing would have happened if stdin was coming
                    > from a non-interactive device (eg. a file), so I don't know
                    > how you can say that it did refer to an interactive device.
                    >
                    >[color=green]
                    >>If scanf's conversion succeeds you will have a newline left in
                    >>your input stream.
                    >>Any subsequent read issued on stdin will encounter this newline
                    >>if you did not enter additional data after some integer that
                    >>corresponds to your conversion specifier.[/color]
                    >
                    >
                    > Subsequent reading will always encounter the newline (eventually).
                    > (I think this is what you intended to say, although on the first
                    > reading, it seemed to me that you were saying that if additional
                    > data was entered then the newline would not be encountered).
                    >[/color]

                    Taking this a step further then, what would be a good but simple method
                    of reading a single character from stdin, evaluating that character (if
                    c=="Y") for example, and then flushing all buffers.
                    Is there a simple way to achieve a yes/no response without requring a CR
                    from the user, thereby presumably negating this problem of /n.

                    Comment

                    • Keith Thompson

                      #11
                      Re: new line character : scanf and getchar

                      T O <TO> writes:
                      [...][color=blue]
                      > Taking this a step further then, what would be a good but simple
                      > method of reading a single character from stdin, evaluating that
                      > character (if c=="Y") for example, and then flushing all buffers.
                      > Is there a simple way to achieve a yes/no response without requring a
                      > CR from the user, thereby presumably negating this problem of /n.[/color]

                      C FAQ 19.1: "How can I read a single character from the keyboard
                      without waiting for the RETURN key? How can I stop characters from
                      being echoed on the screen as they're typed?"

                      <http://www.eskimo.com/~scs/C-faq/q19.1.html>

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

                      Comment

                      • CBFalconer

                        #12
                        Re: new line character : scanf and getchar

                        T O wrote:[color=blue]
                        >[/color]
                        .... snip ...[color=blue]
                        >
                        > Taking this a step further then, what would be a good but simple
                        > method of reading a single character from stdin, evaluating that
                        > character (if c=="Y") for example, and then flushing all buffers.[/color]

                        You could call the following function:

                        /* Returns EOF for file ended, else '\n' */
                        int flushln(FILE *f)
                        {
                        int ch;

                        while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
                        return ch;
                        } /* flushln */
                        [color=blue]
                        > Is there a simple way to achieve a yes/no response without requring a CR
                        > from the user, thereby presumably negating this problem of /n.[/color]

                        No. Without input line buffering there would be no input line
                        editing, which would be a much bigger nuisance than requiring a
                        <cr>.

                        --
                        "If you want to post a followup via groups.google.c om, don't use
                        the broken "Reply" link at the bottom of the article. Click on
                        "show options" at the top of the article, then click on the
                        "Reply" at the bottom of the article headers." - Keith Thompson


                        Comment

                        Working...