473,467 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

int representation incorrect

I am trying to print a simple int output using a for loop and
incrementing a number along the way such as

unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
cout << count << endl;
}

I expect just 0..4 to be printed on the following line but I get
1072693249
1072693250
1072693251
1072693252

why would this be happening?
Apr 5 '06 #1
11 2389
I compiled in the folloiwng environment:
Compiler: mingw
OS: Win 2000 professional

It displayed 0 5 times.

can you tell us a little more about the kind of environment in which
you are compiling?

Apr 5 '06 #2
Are you sure you want to print 'count' in the for loop ? You seem to be
expecting 'i'.
I used follg code and it printed 0....4.

#include <iostream>

main ()
{
unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
std::cout << i << std::endl;

}
}

Apr 5 '06 #3

Steve wrote:
I am trying to print a simple int output using a for loop and
incrementing a number along the way such as

unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
cout << count << endl;
}

I expect just 0..4 to be printed on the following line but I get
1072693249
1072693250
1072693251
1072693252

why would this be happening?


You shuld be getting 0 displayed 5 times. Try again with the code you
have pasted and let us know what you get as output.

If you want 0 to 4 to be displayed then use "i" in cout.

Apr 5 '06 #4

kv********@gmail.com wrote:
Are you sure you want to print 'count' in the for loop ? You seem to be
expecting 'i'.
I used follg code and it printed 0....4.

#include <iostream>

main ()
main() should return int.
{
unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
std::cout << i << std::endl;

} return 0; }


Apr 5 '06 #5
Steve wrote:
I am trying to print a simple int output using a for loop and
incrementing a number along the way such as

unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
cout << count << endl;
}

I expect just 0..4 to be printed on the following line but I get
1072693249
1072693250
1072693251
1072693252

why would this be happening?


Sorry, the code looks more like this. I initialize the count variable
then some other lines of code are executed which do not access or change
the count variable and then the for loop is executed. I had a mistake
before it should have been cout << count++ << endl;

unsigned int count = 0;
.... code that does not directly change count ....
for( int i = 0; i < 5; i++ ){
cout << count++ << endl;
}
Apr 5 '06 #6

Steve wrote:
Steve wrote:
I am trying to print a simple int output using a for loop and
incrementing a number along the way such as

unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
cout << count << endl;
}

I expect just 0..4 to be printed on the following line but I get
1072693249
1072693250
1072693251
1072693252

why would this be happening?


Sorry, the code looks more like this. I initialize the count variable
then some other lines of code are executed which do not access or change
the count variable and then the for loop is executed. I had a mistake
before it should have been cout << count++ << endl;

unsigned int count = 0;
... code that does not directly change count ....
for( int i = 0; i < 5; i++ ){
cout << count++ << endl;
}


You again seem to be missing something. If I try your code snippet, I
do get 0-4. Are you sure you are displaying the count variable ? There
might be a typo and you may by mistake be referring to a variable
declared with a similar name (cnt, etc..) which has been declared but
not initalised.

Probably pasting the code would help us all. Sorry cannot be of much
help.

Apr 5 '06 #7
Steve wrote:
Steve wrote:
I am trying to print a simple int output using a for loop and
incrementing a number along the way such as

unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
cout << count << endl;
}

I expect just 0..4 to be printed on the following line but I get
1072693249
1072693250
1072693251
1072693252

why would this be happening?
Sorry, the code looks more like this. I initialize the count variable
then some other lines of code are executed which do not access or change
the count variable and then the for loop is executed. I had a mistake
before it should have been cout << count++ << endl;

unsigned int count = 0;
... code that does not directly change count ....


And I bet you, this is where your bug is hiding.

Also, if this code does not change count, why do you declare and initialize
count before this point?
for( int i = 0; i < 5; i++ ){
cout << count++ << endl;
}

Best

Kai-Uwe Bux
Apr 5 '06 #8
Because it's unsigned, maybe if you substract from count, you will get
big values, like:

unsigned int count = 0;
for( int i = 0; i < 5; i++ )
{
cout << --count << endl;
}

=>
4294967295
4294967294
4294967293
4294967292
4294967291

Apr 5 '06 #9
Jaspreet wrote:
kv********@gmail.com wrote:
Are you sure you want to print 'count' in the for loop ? You seem to be
expecting 'i'.
I used follg code and it printed 0....4.

#include <iostream>

main ()


main() should return int.
{
unsigned int count = 0;
for( int i = 0; i < 5; i++ ){
std::cout << i << std::endl;

}


return 0;


// if we're going to be pedantic, then that return
// statement is not required.
}

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Apr 5 '06 #10
Steve wrote:
I am trying to print a simple int output using a for loop and
incrementing a number along the way such as


Stop posting code that does not demonstrate your problem, it's a waste
of everybodies time.

Write a complete, compilable program, in your favourite editor. Compile
it. If it demonstrates your problem, THEN copy and paste it here.

Only then can we help.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Apr 5 '06 #11
asterisc wrote:
Because it's unsigned, maybe if you substract from count, you will get
big values, like:

unsigned int count = 0;
for( int i = 0; i < 5; i++ )
{
cout << --count << endl;
}

=>
4294967295
4294967294
4294967293
4294967292
4294967291


Did you really mean that as a reply to *my* post?

Well, one cannot tell since you didn't bother to quote.

Please read the FAQ on how to post in this group.
Thanks

Kai-Uwe Bux
Apr 5 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Charles T. | last post by:
Hi, I currently writing a serialize/unserialize architecture. The read/write function will read/write from a binary file. My question is is there some sort on defined standart to use when...
7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
6
by: Brenda | last post by:
We are currently switching to DB2/AIX. I am modifying my sqrs to work on this new platform. (We are currently on Oracle). I am having a problem with an sqr that has a reference to a variable for...
10
by: Mantorok Redgormor | last post by:
I always see posts that involve the representation of integers, where some poster claims that the unerlyding representation of an integer doesn't have to reflect on the actual integer, for example:...
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
11
by: pemo | last post by:
Ambiguous? I have a student who's asked me to explain the following std text (esp. the footnote). 6.2.6.1.5 Certain object representations need not represent a value of the object type. If...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
26
by: Carramba | last post by:
Hi! How can I output value of char or int in binary form with printf(); ? thanx in advance
10
by: Richard Tobin | last post by:
May all-bits-zero be a trap representation for a pointer? What if I calloc() space for a structure containing pointers? -- Richard -- "Consideration shall be given to the need for as many as...
42
by: davidComSee | last post by:
Hi all: char c = 'a'; To get the hex version of the byte c, i can simply use: printf( " %x ",c); But i couldn't find a control letter for binary output.
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.