Puzzle!

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

    Puzzle!

    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?

  • Tak

    #2
    Re: Puzzle!

    On 6 2 , 1 15 , Tak <kakat...@gmail .comwrote:
    Some days ago, I written a mini program like this:
    #include <stdio.h>
    >
    int main()
    {
    char c;
    a mistake: "int c" not "char c"
    int n;
    >
    scanf("%d",&n);
    c = getchar();
    >
    return 0;
    >
    }

    Comment

    • Walter Roberson

      #3
      Re: Puzzle!

      In article <1180718112.979 701.75740@i13g2 000prf.googlegr oups.com>,
      Tak <kakataka@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?
      while ( (c = getchar()) != EOF && c == '\n' ) { /* empty loop body */ }

      Afterwards, c will either be EOF or a character other than \n .
      This takes into account that the user might press return several times
      before entering the desired character.

      But to make this work, you will have to fix the problem the other poster
      pointed out: that c should be of type int, not of type char.

      --
      Prototypes are supertypes of their clones. -- maplesoft

      Comment

      • Keith Thompson

        #4
        Re: Puzzle!

        Tak <kakataka@gmail .comwrites:
        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?
        (You've already mentioned in a followup that c needs to be an int.)

        If you type "5x" and then <enter>, the program will assign 5 to n and
        'x' to c.

        In a real program, that would be a very unfriendly input format. If
        you must use scanf, you should (a) check its return value, and (b) be
        aware of the state the input stream will be in after the call, and act
        accordingly; for example, you might want to read and ignore all
        characters up to and including a newline (and don't forget to check
        for EOF).

        A better way to handle input is to read a line at a time using
        fgets(), and then analyze the input line once you have 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."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • CBFalconer

          #5
          Re: Puzzle!

          Walter Roberson wrote:
          >
          .... snip ...
          >
          while ( (c = getchar()) != EOF && c == '\n' ) { /* empty loop body */ }
          ^^
          {should be !=) :-)
          >
          Afterwards, c will either be EOF or a character other than \n .
          This takes into account that the user might press return several times
          before entering the desired character.
          --
          <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

          • Mark McIntyre

            #6
            Re: Puzzle!

            On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
            <kakataka@gmail .comwrote:
            >Some days ago, I written a mini program like this:
            >#include <stdio.h>
            >
            > scanf("%d",&n);
            > c = getchar();
            >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?
            Don't use scanf.
            --
            Mark McIntyre

            "Debugging is twice as hard as writing the code in the first place.
            Therefore, if you write the code as cleverly as possible, you are,
            by definition, not smart enough to debug it."
            --Brian Kernighan

            Comment

            • Tak

              #7
              Re: Puzzle!

              On 6 2 , 7 26 , Mark McIntyre <markmcint...@s pamcop.netwrote :
              On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
              >
              <kakat...@gmail .comwrote:
              Some days ago, I written a mini program like this:
              #include <stdio.h>
              >
              scanf("%d",&n);
              c = getchar();
              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?
              >
              Don't use scanf.
              Then use what?


              Comment

              • Army1987

                #8
                Re: Puzzle!


                "Mark McIntyre" <markmcintyre@s pamcop.netha scritto nel messaggio
                news:hma163d8u8 t50m0rqrpcem1lc obheh4l5r@4ax.c om...
                On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
                <kakataka@gmail .comwrote:
                >
                >>Some days ago, I written a mini program like this:
                >>#include <stdio.h>
                >>
                >scanf("%d",&n) ;
                >c = getchar();
                >
                >>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?
                >
                Don't use scanf.
                A little too extreme...
                For example:

                do {
                int k = scanf("%d", &n);
                switch (k) {
                case EOF:
                /* stdin is finished or there was something wrong. */
                /* React accordingly. */
                break;
                case 0:
                puts("Please enter an integer");
                /* maybe write that to stderr? */
                scanf("%*[^\n]%*c"); /*skip line*/
                continue; /* yes, here break and continue would do the */
                /* same thing... :-) */
                case 1:
                break;
                } while (k < 0);

                Or to avoid strange problem if the number input is too large:

                #include <limits.h>
                #include <errno.h>
                do {
                long tmp;
                int k = scanf("%ld", &tmp);
                switch (k) {
                case EOF:
                /* stdin is finished or there was something wrong. */
                /* React accordingly. */
                break;
                case 0:
                puts("Please enter an integer");
                /* maybe write that to stderr? */
                scanf("%*[^\n]%*c"); /*skip line*/
                continue; /* yes, here break and continue would do the */
                /* same thing... :-) */
                case 1:
                if (tmp < INT_MIN || tmp INT_MAX) {
                k = tmp < 0 ? INT_MIN : INT_MAX;
                errno = ERANGE;
                } else
                k = tmp;
                break;
                } while (k < 0);


                Comment

                • Flash Gordon

                  #9
                  Re: Puzzle!

                  Tak wrote, On 02/06/07 04:31:
                  On 6 2 , 7 26 , Mark McIntyre <markmcint...@s pamcop.netwrote :
                  >On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
                  >>
                  ><kakat...@gmai l.comwrote:
                  >>Some days ago, I written a mini program like this:
                  >>#include <stdio.h>
                  >> scanf("%d",&n);
                  >> c = getchar();
                  >>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?
                  >Don't use scanf.
                  >
                  Then use what?
                  fgets (NOT gets, NEVER gets), getchar, getc, fgetc as appropriate. There
                  are gotchas on these so you need to read the documentation and search
                  the groups and the FAQ for further information.
                  --
                  Flash Gordon

                  Comment

                  • Tak

                    #10
                    Re: Puzzle!

                    On 6 2 , 3 58 , "Army1987" <please....@for .itwrote:
                    "Mark McIntyre" <markmcint...@s pamcop.netha scritto nel messaggionews:h ma163d8u8t50m0r qrpcem1lcobheh4 l5r@4ax.com...
                    >
                    <snip>
                    scanf("%*[^\n]%*c"); /*skip line*/
                    I've learnt that statement /scanf("%[^.]", str); /is usually dangerous
                    in some conditions.



                    Comment

                    • Tak

                      #11
                      Re: Puzzle!

                      On 6 2 , 3 39 , Flash Gordon <s...@flash-gordon.me.ukwro te:
                      Tak wrote, On 02/06/07 04:31:
                      >
                      On 6 2 , 7 26 , Mark McIntyre <markmcint...@s pamcop.netwrote :
                      On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
                      Then use what?
                      >
                      fgets (NOT gets, NEVER gets), getchar, getc, fgetc as appropriate. There
                      are gotchas on these so you need to read the documentation and search
                      the groups and the FAQ for further information.

                      I solve problems from ACM-ICPC, so fgets fgetc are not useful.
                      usually I solved like this :
                      int main()
                      {
                      int c;
                      int n;

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

                      return 0;

                      }

                      Comment

                      • Harald van =?UTF-8?B?RMSzaw==?=

                        #12
                        Re: Puzzle!

                        Tak wrote:
                        On 6 2 , 3 58 , "Army1987" <please....@for .itwrote:
                        >"Mark McIntyre" <markmcint...@s pamcop.netha scritto nel
                        >messaggionews: hma163d8u8t50m0 rqrpcem1lcobheh 4l5r@4ax.com...
                        >>
                        <snip>
                        > scanf("%*[^\n]%*c"); /*skip line*/
                        >
                        I've learnt that statement /scanf("%[^.]", str); /is usually dangerous
                        in some conditions.
                        You've learnt correctly, but that's not what's happening here. The problem
                        with scanf("%[^.]", str); is that you cannot know in advance whether enough
                        memory is available to store the string in str. scanf("%*[^\n]%*c");
                        discards everything it reads, instead of storing it.

                        Comment

                        • Army1987

                          #13
                          Re: Puzzle!


                          "Tak" <kakataka@gmail .comha scritto nel messaggio
                          news:1180773802 .789151.42330@a 26g2000pre.goog legroups.com...
                          I solve problems from ACM-ICPC, so fgets fgetc are not useful.
                          usually I solved like this :
                          int main()
                          {
                          int c;
                          int n;
                          >
                          scanf("%d",&n);
                          c = getchar();
                          c = getchar();
                          >
                          return 0;
                          >
                          }
                          This is ok only as long as you know the only character between one number
                          and the character to be read will be the newline.
                          BTW, why do you assign c in both calls of getchar()? Isn't
                          getchar(); c = getchar(); ok?


                          Comment

                          • Tak

                            #14
                            Re: Puzzle!

                            On 6 2 , 7 21 , "Army1987" <please....@for .itwrote:
                            "Tak" <kakat...@gmail .comha scritto nel messaggionews:1 180773802.78915 1.42330@a26g200 0pre.googlegrou ps.com...
                            >
                            I solve problems from ACM-ICPC, so fgets fgetc are not useful.
                            usually I solved like this :
                            int main()
                            {
                            int c;
                            int n;
                            >
                            scanf("%d",&n);
                            c = getchar();
                            c = getchar();
                            >
                            return 0;
                            >
                            }
                            >
                            This is ok only as long as you know the only character between one number
                            and the character to be read will be the newline.
                            BTW, why do you assign c in both calls of getchar()? Isn't
                            getchar(); c = getchar(); ok?
                            Yes , it seems cleaner.

                            Comment

                            • CBFalconer

                              #15
                              Re: Puzzle!

                              Tak wrote:
                              >
                              .... snip ...
                              >
                              I solve problems from ACM-ICPC, so fgets fgetc are not useful.
                              usually I solved like this :
                              >
                              int main() {
                              int c;
                              int n;
                              >
                              scanf("%d",&n);
                              c = getchar();
                              c = getchar();
                              return 0;
                              }
                              I have no idea what ACM-ICPC is. However fgets, fgetc, getc, etc.
                              are all available in any standard C compliant system. That is one
                              of the purposes of having a standard.

                              scanf is generally not too useful in interactive work, and should
                              never be used without checking its return value.

                              --
                              <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

                              Working...