Help! help!

Discussion in 'C++' started by questions, Oct 25, 2008.

  1. questions

    questions Guest

    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;}
     
    questions, Oct 25, 2008
    #1
    1. Advertisements

  2. questions

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Oct 25, 2008
    #2
    1. Advertisements

  3. questions

    Rolf Magnus Guest

    Well, depending on the target platform, the range of int can be as low
    as -32767 ... +32767.
     
    Rolf Magnus, Oct 25, 2008
    #3
  4. questions

    Andre Kostur Guest

    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).
     
    Andre Kostur, Oct 25, 2008
    #4
  5. questions

    Lightning Guest

    To elaborate on the post above, just make x,y,z,m,n,w long.
     
    Lightning, Oct 26, 2008
    #5
  6. questions

    James Kanze Guest

    On what machine? I'll suppose a 16 bit machine, since
    otherwise, 78965 is not too big.
    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, Oct 26, 2008
    #6
  7. questions

    peter koch Guest

    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
     
    peter koch, Oct 27, 2008
    #7
  8. questions

    osmium Guest

    :

    <quote>
    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.
     
    osmium, Oct 27, 2008
    #8
  9. questions

    Default User Guest

    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
     
    Default User, Oct 27, 2008
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.