Counting Problem

Started by
7 comments, last by Fruny 18 years, 6 months ago
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;
}



Advertisement
From your code:
 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.
Jooleem. Get addicted.
Yeah, remember, a modulus is really two operations, and one's a division.
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;}
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)'.

----------------------#include "signature.h"
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".
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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???

for(int i = 0; i < UINT_MAX - 100; i += 100) {     cout << i << endl;}

Is that any good?
Quote:#include <iostream.h>


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.





"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement