#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;
}
Counting Problem
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:
From your code:
From MSDN:
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.
oh sorry ... it was supposed to be "i%10"
the amended code now:
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)'.
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 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???
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement