Help! help!

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

    #1

    Help! help!

    when I type 12456,it will give me "1 2 4 5 6".But when the integer
    is too great ,for example ,if I type 78965,it will not give me "7 8
    9 6 5",but give me a wrong result,why????

    #include<stdio. h>
    int main()
    { int x,y,z,m,n,w;

    printf("Give me an integer\n");
    scanf("%d",&x);

    y=x/10000;
    z=x%10000/1000;
    m=x%1000/100;
    n=x%100/10;
    w=x%10;

    printf("%d %d %d %d %d",y,z,m,n,w );

    return 0;}
  • Kai-Uwe Bux

    #2
    Re: Help! help!

    questions wrote:
    when I type 12456,it will give me "1 2 4 5 6".But when the integer
    is too great ,for example ,if I type 78965,it will not give me "7 8
    9 6 5",but give me a wrong result,why????
    >
    #include<stdio. h>
    int main()
    { int x,y,z,m,n,w;
    >
    printf("Give me an integer\n");
    scanf("%d",&x);
    >
    y=x/10000;
    z=x%10000/1000;
    m=x%1000/100;
    n=x%100/10;
    w=x%10;
    >
    printf("%d %d %d %d %d",y,z,m,n,w );
    >
    return 0;}
    Just a data point: when I try your code with g++ version 4.3.1 and 78965 as
    input, I get exactly

    7 8 9 6 5

    as the output.


    Best

    Kai-Uwe Bux

    Comment

    • Rolf Magnus

      #3
      Re: Help! help!

      questions wrote:
      when I type 12456,it will give me "1 2 4 5 6".But when the integer
      is too great ,for example ,if I type 78965,it will not give me "7 8
      9 6 5",but give me a wrong result,why????
      Well, depending on the target platform, the range of int can be as low
      as -32767 ... +32767.

      Comment

      • Andre Kostur

        #4
        Re: Help! help!

        questions <mengqidhuang@y ahoo.cnwrote in news:5e98b478-4415-4d99-b4da-
        31ee5e75591b@e3 8g2000prn.googl egroups.com:
        when I type 12456,it will give me "1 2 4 5 6".But when the integer
        is too great ,for example ,if I type 78965,it will not give me "7 8
        9 6 5",but give me a wrong result,why????
        >
        #include<stdio. h>
        int main()
        { int x,y,z,m,n,w;
        >
        printf("Give me an integer\n");
        scanf("%d",&x);
        >
        y=x/10000;
        z=x%10000/1000;
        m=x%1000/100;
        n=x%100/10;
        w=x%10;
        >
        printf("%d %d %d %d %d",y,z,m,n,w );
        >
        return 0;}
        >
        Because an int can only hold a certain size of number. The acutal size is
        dependant on your platform. You do know that it can hold at least as large
        as a short, and no more than a long. (Offhand it seems that your int is a
        16-bit value, so the max number here is 32767. int's are signed so the
        full range would be -32768 - 32767).

        Comment

        • Lightning

          #5
          Re: Help! help!

          On Oct 26, 2:33 am, Rolf Magnus <ramag...@t-online.dewrote:
          questions wrote:
          when I type 12456,it will give me "1  2  4  5  6".But when the integer
          is too great ,for example ,if I type 78965,it will not give me  "7  8
          9  6  5",but give me a wrong result,why????
          >
          Well, depending on the target platform, the range of int can be as low
          as -32767 ... +32767.
          To elaborate on the post above, just make x,y,z,m,n,w long.

          Comment

          • James Kanze

            #6
            Re: Help! help!

            On Oct 25, 11:51 am, questions <mengqidhu...@y ahoo.cnwrote:
            when I type 12456,it will give me "1  2  4  5  6".But when the
            integer is too great ,for example ,if I type 78965,it will not
            give me  "7  8 9  6  5",but give me a wrong result,why????
            On what machine? I'll suppose a 16 bit machine, since
            otherwise, 78965 is not too big.
            #include<stdio. h>
            int main()
            { int x,y,z,m,n,w;
              printf("Give me an integer\n");
              scanf("%d",&x);
            If the value read isn't representable in a int, you have
            undefined behavior here. The next version of C++ will define
            it strictly for operator>>. And from a QoI point of view, I
            would expect defined behavior as well, with x being set to
            MAX_INT (or MIN_INT, if the value was negative), and an error
            being generated. (Of course, since you don't test the return
            value of scanf, even if it does detect the error, you wouldn't
            know.)

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • peter koch

              #7
              Re: Help! help!

              On 25 Okt., 15:46, Andre Kostur <an...@kostur.n etwrote:
              questions <mengqidhu...@y ahoo.cnwrote in news:5e98b478-4415-4d99-b4da-
              31ee5e755...@e3 8g2000prn.googl egroups.com:
              >
              >
              >
              >
              >
              when I type 12456,it will give me "1  2  4  5  6".But when the integer
              is too great ,for example ,if I type 78965,it will not give me  "7  8
              9  6  5",but give me a wrong result,why????
              >
              #include<stdio. h>
              int main()
              { int x,y,z,m,n,w;
              >
                printf("Give me an integer\n");
                scanf("%d",&x);
              >
                y=x/10000;
                z=x%10000/1000;
                m=x%1000/100;
                n=x%100/10;
                w=x%10;
              >
                printf("%d  %d  %d  %d  %d",y,z,m,n,w );
              >
                return 0;}
              >
              Because an int can only hold a certain size of number.  The acutal sizeis
              dependant on your platform.  You do know that it can hold at least as large
              as a short, and no more than a long.  (Offhand it seems that your int is a
              16-bit value, so the max number here is 32767.  int's are signed so the
              full range would be -32768 - 32767).
              I agree with your diagnosis. but it is still a little weird: I would
              suspect that most developers today would either be working on a
              platform where an integer is 32 bits or be sufficiently knowledgeable
              that they would know the answer to the problem immediately.

              /Peter

              Comment

              • osmium

                #8
                Re: Help! help!

                "peter koch" wrote:

                <quote>
                On 25 Okt., 15:46, Andre Kostur <an...@kostur.n etwrote:
                questions <mengqidhu...@y ahoo.cnwrote in news:5e98b478-4415-4d99-b4da-
                31ee5e755...@e3 8g2000prn.googl egroups.com:
                >
                >
                >
                >
                >
                when I type 12456,it will give me "1 2 4 5 6".But when the integer
                is too great ,for example ,if I type 78965,it will not give me "7 8
                9 6 5",but give me a wrong result,why????
                >
                #include<stdio. h>
                int main()
                { int x,y,z,m,n,w;
                >
                printf("Give me an integer\n");
                scanf("%d",&x);
                >
                y=x/10000;
                z=x%10000/1000;
                m=x%1000/100;
                n=x%100/10;
                w=x%10;
                >
                printf("%d %d %d %d %d",y,z,m,n,w );
                >
                return 0;}
                >
                Because an int can only hold a certain size of number. The acutal size is
                dependant on your platform. You do know that it can hold at least as large
                as a short, and no more than a long. (Offhand it seems that your int is a
                16-bit value, so the max number here is 32767. int's are signed so the
                full range would be -32768 - 32767).
                I agree with your diagnosis. but it is still a little weird: I would
                suspect that most developers today would either be working on a
                platform where an integer is 32 bits or be sufficiently knowledgeable
                that they would know the answer to the problem immediately.
                <end quote>

                My guess would be that the person who asked the question was not, by any
                stretch of the imagination, a developer.


                Comment

                • Default User

                  #9
                  Re: Help! help!

                  osmium wrote:
                  "peter koch" wrote:
                  >
                  <quote>
                  <end quote>
                  Did I mention OE Quotefix for fixing the quoting Google Groups problem
                  to you already? I know it came up once before, but I can't recall if it
                  was you.




                  Brian

                  Comment

                  Working...