Jump to content
  • Advertisement
Sign in to follow this  
BaldPython

Counting Problem

This topic is 4701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!