Puzzle!

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

    #46
    Re: Puzzle!

    In article <1180997619.081 045.144450@d30g 2000prg.googleg roups.com>,
    Old Wolf <oldwolf@inspir e.net.nzwrote:
    >On Jun 2, 10:17 am, CBFalconer <cbfalco...@yah oo.comwrote:
    >Walter Roberson wrote:
    while ( (c = getchar()) != EOF && c == '\n' )
    { /* empty loop body */ }
    > ^^
    > {should be !=) :-)
    >The original was correct; the purpose of the code
    >is to consume '\n' characters.
    Exactly -- and my posting specifically indicated that as the
    intended behaviour.

    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer

    Comment

    • Tak

      #47
      Re: Puzzle!

      On 6ÔÂ4ÈÕ, ÏÂÎç11ʱ33·Ö, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
      wrote:
      I don't know.when I use fgets,fgetc the system return me a Wrong
      Answer. Maybe because the test data is not stored in a file,so we
      can't use fgets,fgetc to get data?
      >
      Your response hints to me that you did not use fgets() or fgetc()
      properly. The standard C library does not operate on "files", it
      operates on "streams". It is entirely possible with C that one
      run of a program will have a stream attached to a file and the
      next run with the stream attached to a terminal, without any changes
      in the code.
      >
      The key to using fgets() or fgetc() with the standard input stream
      is to supply the FILE* parameter as the name stdin such as
      >
      if ( fgets(&mybuffer , sizeof mybuffer, stdin) != NULL ) { ... }
      >
      can you tell me how to deal with this "a+b problem" by /fgets/ in your
      format?
      P.S.
      it's not homework.
      the a+b problem:
      Input:
      The input will consist of a series of pairs of integers a and
      b,separated by a space, one pair of integers per line.
      Output:
      For each pair of input integers a and b you should output the sum of a
      and b in one line,and with one line of output for each line in input.

      Comment

      • Flash Gordon

        #48
        Re: Puzzle!

        Tak wrote, On 05/06/07 11:05:
        On 6ÔÂ4ÈÕ, ÏÂÎç11ʱ33·Ö, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
        wrote:
        >>I don't know.when I use fgets,fgetc the system return me a Wrong
        >>Answer. Maybe because the test data is not stored in a file,so we
        >>can't use fgets,fgetc to get data?
        >Your response hints to me that you did not use fgets() or fgetc()
        >properly. The standard C library does not operate on "files", it
        >operates on "streams". It is entirely possible with C that one
        >run of a program will have a stream attached to a file and the
        >next run with the stream attached to a terminal, without any changes
        >in the code.
        >>
        >The key to using fgets() or fgetc() with the standard input stream
        >is to supply the FILE* parameter as the name stdin such as
        >>
        >if ( fgets(&mybuffer , sizeof mybuffer, stdin) != NULL ) { ... }
        >>
        can you tell me how to deal with this "a+b problem" by /fgets/ in your
        format?
        P.S.
        it's not homework.
        You've still not attempted it though.
        the a+b problem:
        Input:
        The input will consist of a series of pairs of integers a and
        b,separated by a space, one pair of integers per line.
        Output:
        For each pair of input integers a and b you should output the sum of a
        and b in one line,and with one line of output for each line in input.
        Parse the lines with the parser of your choice, strtol for example, sum
        the data and output it. Nothing difficult. If you want serious help make
        a serious attempt yourself and post it.
        --
        Flash Gordon

        Comment

        • CBFalconer

          #49
          Re: Puzzle!

          Tak wrote:
          >
          .... snip ...
          >
          can you tell me how to deal with this "a+b problem" by /fgets/ in
          your format?
          >
          it's not homework. the a+b problem:
          Input:
          The input will consist of a series of pairs of integers a and
          b,separated by a space, one pair of integers per line.
          >
          Output:
          For each pair of input integers a and b you should output the sum
          of a and b in one line,and with one line of output for each line
          in input.
          The input is in text form. You have to convert that to integers
          before adding. Then you convert the result back into text. The
          first phase can be handled by scanf, or (fgets or ggets) and
          (sscanf or strtod or strtoul). The second by printf. Read up on
          what those functions do. The prefereable place to read is the C
          standard. ggets is non-standard, available on my page.

          --
          <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
          <http://www.securityfoc us.com/columnists/423>
          <http://www.aaxnet.com/editor/edit043.html>
          <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
          cbfalconer at maineline dot net



          --
          Posted via a free Usenet account from http://www.teranews.com

          Comment

          • Army1987

            #50
            Re: Puzzle!


            "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.caha scritto nel messaggio
            news:f427j8$mul $1@canopus.cc.u manitoba.ca...
            In article <1180997619.081 045.144450@d30g 2000prg.googleg roups.com>,
            Old Wolf <oldwolf@inspir e.net.nzwrote:
            >>On Jun 2, 10:17 am, CBFalconer <cbfalco...@yah oo.comwrote:
            >>Walter Roberson wrote:
            >
            >while ( (c = getchar()) != EOF && c == '\n' )
            > { /* empty loop body */ }
            >> ^^
            >> {should be !=) :-)
            >
            >>The original was correct; the purpose of the code
            >>is to consume '\n' characters.
            >
            Exactly -- and my posting specifically indicated that as the
            intended behaviour.
            >
            Since '\n' != EOF, couldn't you just write
            while (getchar() == '\n)
            continue;
            ?


            Comment

            • Walter Roberson

              #51
              Re: Puzzle!

              In article <f4bi1h$tkc$1@t di.cu.mi.it>, Army1987 <please.ask@for .itwrote:
              >>>Walter Roberson wrote:
              >>while ( (c = getchar()) != EOF && c == '\n' )
              >> { /* empty loop body */ }
              >Since '\n' != EOF, couldn't you just write
              >while (getchar() == '\n)
              continue;
              Good point, a seperate EOF test is not needed. But you do need to
              capture the character that was read, as otherwise you end up discarding
              the first non- EOF .

              while ( (c = getchar()) == '\n' ) continue; /* consumes newlines */
              --
              There are some ideas so wrong that only a very intelligent person
              could believe in them. -- George Orwell

              Comment

              • BiGYaN

                #52
                Re: Puzzle!

                On Jun 1, 10:15 pm, Tak <kakat...@gmail .comwrote:
                Some days ago, I written a mini program like this:
                #include <stdio.h>
                >
                int main()
                {
                char c;
                int n;
                >
                scanf("%d",&n);
                c = getchar();
                >
                return 0;
                >
                }
                >
                I want to assign n and c specific values,for example,give n and c
                values of 5 and 'x' in fact when I input 5 then "enter",the program
                is finish.I know when I press "Enter", I assigned '\n' to variable c,
                But how can I avoid this?


                How about this one :

                int main()
                {
                char c;
                int n;

                scanf("%d",&n);
                fflush(stdin);
                c = getchar();

                return 0;
                }

                fflush(stdin) just flushes out the "\n" from the buffer.

                Comment

                • Default User

                  #53
                  Re: Puzzle!

                  BiGYaN wrote:

                  fflush(stdin) just flushes out the "\n" from the buffer.
                  No, it doesn't. It is undefined behavior. Some implementations provide
                  extensions that do what you say, but many other don't.




                  Brian

                  Comment

                  • Keith Thompson

                    #54
                    Re: Puzzle!

                    "Default User" <defaultuserbr@ yahoo.comwrites :
                    BiGYaN wrote:
                    >fflush(stdin ) just flushes out the "\n" from the buffer.
                    >
                    No, it doesn't. It is undefined behavior. Some implementations provide
                    extensions that do what you say, but many other don't.
                    One system that I know of (Solaris) defines the behavior of
                    fflush(stdin) to discard any buffered input. I don't know of any
                    implementations on which it 'just flushes out the "\n"'. (Discarding
                    all input up to the next '\n' is easy enough to do without using
                    fflush().)

                    See questins 12.26a and 12.26b in the comp.lang.c FAQ,
                    <http://www.c-faq.com/>.

                    --
                    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."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Richard Heathfield

                      #55
                      Re: Puzzle!

                      BiGYaN said:

                      <snip>
                      How about this one :
                      >
                      int main()
                      {
                      char c;
                      int n;
                      >
                      scanf("%d",&n);
                      Calling a variadic function without a valid prototype in scope invokes
                      undefined behaviour. To fix this, #include <stdio.hYou also fail to
                      check that your request for an integer value succeeded.
                      fflush(stdin);
                      stdin is undefined. If you define it appropriately by #include <stdio.h>
                      you then run into the problem that the behaviour of fflush is defined
                      only for streams open for output or update. stdin is neither.
                      c = getchar();
                      getchar returns int, not char.

                      To fit so many fundamental errors into such a short program, presumably
                      without trying to, is truly impressive.

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at the above domain, - www.

                      Comment

                      • Default User

                        #56
                        Re: Puzzle!

                        Keith Thompson wrote:
                        "Default User" <defaultuserbr@ yahoo.comwrites :
                        BiGYaN wrote:
                        fflush(stdin) just flushes out the "\n" from the buffer.
                        No, it doesn't. It is undefined behavior. Some implementations
                        provide extensions that do what you say, but many other don't.
                        >
                        One system that I know of (Solaris) defines the behavior of
                        fflush(stdin) to discard any buffered input. I don't know of any
                        implementations on which it 'just flushes out the "\n"'. (Discarding
                        all input up to the next '\n' is easy enough to do without using
                        fflush().)
                        I made the assumption, perhaps incorrectly, that the person explained
                        it badly. At that point, the only thing left in the buffer would be the
                        \n. I didn't take to mean that the person thought that fflush(stdin)
                        always would remove the \n and leave other characters. Frankly, I think
                        that reading of the post strains hard to make something of nothing.

                        See questins 12.26a and 12.26b in the comp.lang.c FAQ,
                        <http://www.c-faq.com/>.
                        Is this supposed to be for me? If so, why?



                        Brian

                        Comment

                        • CBFalconer

                          #57
                          Re: Puzzle!

                          BiGYaN wrote:
                          >
                          .... snip ...
                          >
                          How about this one :
                          >
                          int main() {
                          char c;
                          int n;
                          >
                          scanf("%d",&n);
                          fflush(stdin);
                          c = getchar();
                          return 0;
                          }
                          >
                          fflush(stdin) just flushes out the "\n" from the buffer.
                          No it doesn't. It incites UB. fflush only works on output files.
                          Try:

                          int flushln(FILE *f) {
                          int ch;

                          while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
                          return ch;
                          }

                          --
                          <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                          <http://www.securityfoc us.com/columnists/423>
                          <http://www.aaxnet.com/editor/edit043.html>
                          <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                          cbfalconer at maineline dot net



                          --
                          Posted via a free Usenet account from http://www.teranews.com

                          Comment

                          • BiGYaN

                            #58
                            Re: Puzzle!

                            On Jun 10, 1:38 am, Keith Thompson <k...@mib.orgwr ote:
                            "Default User" <defaultuse...@ yahoo.comwrites :
                            BiGYaN wrote:
                            fflush(stdin) just flushes out the "\n" from the buffer.
                            >
                            No, it doesn't. It is undefined behavior. Some implementations provide
                            extensions that do what you say, but many other don't.
                            >
                            One system that I know of (Solaris) defines the behavior of
                            fflush(stdin) to discard any buffered input. I don't know of any
                            implementations on which it 'just flushes out the "\n"'. (Discarding
                            all input up to the next '\n' is easy enough to do without using
                            fflush().)
                            >
                            See questins 12.26a and 12.26b in the comp.lang.c FAQ,
                            <http://www.c-faq.com/>.
                            >
                            --
                            Keith Thompson (The_Other_Keit h) k...@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."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            @ Keith

                            Actually you interpreted it wrongly. What I wanted to say was that
                            fflush(stdin) will flush out the whole buffer. But in this case the
                            buffer had only a "\n". So I wrote "fflush(std in) just flushes out the
                            "\n" from the buffer".

                            Hope you got my point now.

                            BTW, "Default User" has already clarified my point of view.

                            Comment

                            • BiGYaN

                              #59
                              Re: Puzzle!

                              On Jun 10, 2:55 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                              BiGYaN said:
                              >
                              <snip>
                              >
                              How about this one :
                              >
                              int main()
                              {
                              char c;
                              int n;
                              >
                              scanf("%d",&n);
                              >
                              Calling a variadic function without a valid prototype in scope invokes
                              undefined behaviour. To fix this, #include <stdio.hYou also fail to
                              check that your request for an integer value succeeded.
                              >
                              fflush(stdin);
                              >
                              stdin is undefined. If you define it appropriately by #include <stdio.h>
                              you then run into the problem that the behaviour of fflush is defined
                              only for streams open for output or update. stdin is neither.
                              >
                              c = getchar();
                              >
                              getchar returns int, not char.
                              >
                              To fit so many fundamental errors into such a short program, presumably
                              without trying to, is truly impressive.
                              >
                              --
                              Richard Heathfield
                              "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
                              email: rjh at the above domain, - www.

                              I could never imagine writing a program in C without "#include
                              <stdio.h>". I just did not write it as I thought it was quite obvious
                              to add it at the start. But I'm sorry if it caused some inconvenience
                              to my not-so-learned friend.

                              As far as getchar() returning an int. Yes I know it does, but why
                              bother with that here. The original question asked for a solution and
                              I provided it. It will work fine in this case.

                              Comment

                              • Joachim Schmitz

                                #60
                                Re: Puzzle!

                                "BiGYaN" <bigyan.techie@ gmail.comschrie b im Newsbeitrag
                                news:1181460883 .888597.219720@ n15g2000prd.goo glegroups.com.. .
                                On Jun 10, 1:38 am, Keith Thompson <k...@mib.orgwr ote:
                                >"Default User" <defaultuse...@ yahoo.comwrites :
                                BiGYaN wrote:
                                >fflush(stdin ) just flushes out the "\n" from the buffer.
                                >>
                                No, it doesn't. It is undefined behavior. Some implementations provide
                                extensions that do what you say, but many other don't.
                                >>
                                >One system that I know of (Solaris) defines the behavior of
                                >fflush(stdin ) to discard any buffered input. I don't know of any
                                >implementation s on which it 'just flushes out the "\n"'. (Discarding
                                >all input up to the next '\n' is easy enough to do without using
                                >fflush().)
                                >>
                                >See questins 12.26a and 12.26b in the comp.lang.c FAQ,
                                ><http://www.c-faq.com/>.
                                >>
                                >--
                                >Keith Thompson (The_Other_Keit h) k...@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."
                                > -- Antony Jay and Jonathan Lynn, "Yes Minister"
                                >
                                >
                                @ Keith
                                >
                                Actually you interpreted it wrongly. What I wanted to say was that
                                fflush(stdin) will flush out the whole buffer. But in this case the
                                buffer had only a "\n". So I wrote "fflush(std in) just flushes out the
                                "\n" from the buffer".
                                Still fflush() on an input stream invokes the deamons of undefined behavoir

                                Bye, Jojo


                                Comment

                                Working...