[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Help! help!

questions

10/25/2008 10:52:00 AM

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;}
13 Answers

Kai-Uwe Bux

10/25/2008 11:13:00 AM

0

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

Rolf Magnus

10/25/2008 1:33:00 PM

0

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.

Andre Kostur

10/25/2008 2:47:00 PM

0

questions <mengqidhuang@yahoo.cn> wrote in news:5e98b478-4415-4d99-b4da-
31ee5e75591b@e38g2000prn.googlegroups.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).

Lightning

10/25/2008 11:03:00 PM

0

On Oct 26, 2:33 am, Rolf Magnus <ramag...@t-online.de> wrote:
> 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.

James Kanze

10/26/2008 9:58:00 AM

0

On Oct 25, 11:51 am, questions <mengqidhu...@yahoo.cn> 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????

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.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

peter koch

10/27/2008 3:06:00 PM

0

On 25 Okt., 15:46, Andre Kostur <an...@kostur.net> wrote:
> questions <mengqidhu...@yahoo.cn> wrote in news:5e98b478-4415-4d99-b4da-
> 31ee5e755...@e38g2000prn.googlegroups.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.

/Peter

osmium

10/27/2008 4:22:00 PM

0

"peter koch" wrote:

<quote>
On 25 Okt., 15:46, Andre Kostur <an...@kostur.net> wrote:
> questions <mengqidhu...@yahoo.cn> wrote in news:5e98b478-4415-4d99-b4da-
> 31ee5e755...@e38g2000prn.googlegroups.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.


Default User

10/27/2008 5:56:00 PM

0

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

andyk122

5/14/2009 3:18:00 AM

0

On May 13, 9:02 pm, gumboman <gu...@gumbo.com> wrote:
>
> What in the world is a dairy restaurant?


Talk about a dying breed. The dairy restaurant is the opposite of the
kosher deli. All milk, no meat. A dairy restaurant is basically a
kosher vegetarian restaurant where you would get borscht, blintzes,
kasha, latkes, fantastic soups, great onion rolls, gefilte fish,
etc... There are VERY few left. Ratner's was probably the most famous
of them all, down on the lower east side. Sadly, it closed about 10
years ago. Tiny little B&H on 2nd Ave. is the only remaining dairy
restaurant in Manhattan that I'm aware of. There's probably a few in
Brighton Beach or Borough Park.

Now I feel old.



Michael

5/14/2009 3:45:00 AM

0

On 2009-05-13 23:18:23 -0400, andyk122@gmail.com said:

> On May 13, 9:02?pm, gumboman <gu...@gumbo.com> wrote:
>>
>> What in the world is a dairy restaurant?
>
>
> Talk about a dying breed. The dairy restaurant is the opposite of the
> kosher deli. All milk, no meat. A dairy restaurant is basically a
> kosher vegetarian restaurant where you would get borscht, blintzes,
> kasha, latkes, fantastic soups, great onion rolls, gefilte fish,
> etc... There are VERY few left. Ratner's was probably the most famous
> of them all, down on the lower east side. Sadly, it closed about 10
> years ago. Tiny little B&H on 2nd Ave. is the only remaining dairy
> restaurant in Manhattan that I'm aware of. There's probably a few in
> Brighton Beach or Borough Park.
>
> Now I feel old.

Ratner's was horrible. The Grand Street Dairy Restaurant was a great
place, delicious mushroom barley soup, amazing black bread....it was
replaced by an Asian green grocer.