Sign in to follow this  
BaldPython

Counting Problem

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.





Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this