# Counting Problem

## Recommended Posts

BaldPython    100
Hi there ... Actually I was printing counting until the user's desired limit, but the program crashes just as it reaches the limit of "unsigned int" ... so I decided to remove it by using another loop, but I dont know what's wrong with it... It is not entering the second loop ... :( Here's the code:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{

unsigned int i=0,j,k;
cout << i;

while( !kbhit() )
{
while(i!=UINT_MAX)
cout << (i+=100) << endl;

if( (i++) == UINT_MAX )
{
j = i%0;
k = (int)((i/10) + 10);

while(j <= UINT_MAX)
cout << (j++) + k << endl;

}
}

system("PAUSE");
return 0;
}



##### Share on other sites
Peregrin    496
 j = i%0;

From MSDN:
Quote:
 Division by 0 in either a division or a modulus expression is undefined and causes a run-time error.

##### Share on other sites
Kevinator    229
Yeah, remember, a modulus is really two operations, and one's a division.

##### Share on other sites
BaldPython    100
oh sorry ... it was supposed to be "i%10"

the amended code now:
#include <iostream.h>#include <conio.h>#include <stdlib.h>#include <limits.h>int main(){	unsigned int i=0,j,k;	 cout << i;				while( !kbhit() )				 {					 while(i!=UINT_MAX)						  cout << (i+=100) << endl;					 					 if( (i++) == UINT_MAX )                                         {                                             j = i%10;                                             k = (int)((i/10) + 10);                                                while(j <= UINT_MAX)                                                      cout << (j++) + k << endl;                                        }                                  }	  system("PAUSE");      return 0;}

##### Share on other sites
ZuBsPacE    208
while(i!=UINTMAX)  cout << (i+=100) << endl;

careful...

UINT_MAX is 4294967295, but you increase i in steps of 100.

that means, 'i' will be 4294967200 once, and next step it will be out of bounds when it reaches 4294967300. Hmm, I'm not so sure about what happens if a number goes out of its range, but I guess it's undefined behaviour. :)

Moreover 'i' will never be equal to UINTMAX so you cant use 'while(i!=UINTMAX)'.

##### Share on other sites
grekster    640
Like the above poster said, i will never equal UINT_MAX. I believe that when i goes over UINT_MAX it will loop back to its lowest value, so in that situation you have an infinite loop. when you say your program crashes does it give you an error message or does it just "hang".

##### Share on other sites
BaldPython    100
Quote:
 Original post by ZuBsPacE *** Source Snippet Removed ***careful...UINT_MAX is 4294967295, but you increase i in steps of 100.that means, 'i' will be 4294967200 once, and next step it will be out of bounds when it reaches 4294967300. Hmm, I'm not so sure about what happens if a number goes out of its range, but I guess it's undefined behaviour. :)Moreover 'i' will never be equal to UINTMAX so you cant use 'while(i!=UINTMAX)'.

I've changed it to from "i+=10" to "i++" ... but still, its not working. Please anybody can help???

##### Share on other sites
evanofsky    2913
for(int i = 0; i < UINT_MAX - 100; i += 100) {     cout << i << endl;}

Is that any good?

##### Share on other sites
Fruny    1658
Quote:
 #include

No. #include <iostream>. I won't comment on stdlib.h nor limits.h

Quote:
 while(i!=UINT_MAX) cout << (i+=100) << endl;

I doubt UINT_MAX is a multiple of 100.

Quote:
 if( (i++) == UINT_MAX )

Do you really want to perform the test before the increment?

Honestly, you should first write down, on paper, in English, what it is you want your program to do, when your second loop should be entered, etc.