Get rid of the file I/O completely, and paste my code exactly, does it still exhibit the infinite loop?
Get rid of the file I/O completely, and paste my code exactly, does it still exhibit the infinite loop?
Don't try subtle, supposedly innocuous changes.Code:for(i=0;s[i]!=0; i++) { if(s[i]%3 == 0) { printf("found %c to be divisible by 3, summing\n", s[i]); fprintf(f, "found %c to be divisible by 3, summing\n", s[i]); i = s[i] - '0'; sum = sum + i; } }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
What the heck.
I've attached my entire project.
Since the forum doesn't allow .zip extensions, you'll have to rename the file to kin_03.zip after you download it.
Last edited by cunnus88; 02-04-2008 at 01:12 AM.
But still, I think you want to check whether the actual digit is divisible by 3, not simply the character representing it.
Code:#include <stdio.h> int i_summultofthree(int i) { char s[256]; int sum = 0; sprintf(s, "%d", i); for ( i = 0; s[i] != 0; ++i ) { int digit = s[i] - '0'; if ( digit % 3 == 0 ) { sum += digit; } } return sum; } int main() { int i = 123456789; printf("i_summultofthree(%d) = %d\n", i, i_summultofthree(i)); return 0; } /* my output i_summultofthree(123456789) = 18 */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
True, missed that, but it wouldn't be what results in the infinite loop
Here is the output I got when compiling your main.cpp.
Code:123456789found 3 to be divisible by 3, summing found 6 to be divisible by 3, summing found 9 to be divisible by 3, summing 18
Sent from my iPadŽ
I have no idea what's going on, but Sinkula's code worked for me.
And while your suspicions are understandable, I think I can manage a simple ctrl-c then ctrl-v.
And I can assure you, I first tried a simple copy paste execution before I did anything to the code. But cwr's code went through and infinite loop and sinkula's didn't.
What's s[10] of a 9-digit number?Originally Posted by cwr
Last edited by Dave_Sinkula; 10-20-2005 at 10:32 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Your point is correct, Dave, but why did the code compile fine for CRW and me, but not for cunnus?
Sent from my iPadŽ
I don't think I've ever gotten so much excitement out of a post before. The speed was almost like being in a chatroom.
I hope there's a resolution in here somewhere (which I'm not understaning at the moment), but nonetheless, thank you very much, everyone.
Oh, of course. The reason it is breaking is because you were changing the value of i, which is also your iterator, use a different variable, as Dave does. Sorry.
Why was it working fine on our systems, though cwr?
Sent from my iPadŽ
Because it is relying on values that are unitialised (s[10] and beyond), our systems coincidentally hit a zero byte, but the values were undefined.
Huh? I'm afraid I don't understand. How could using the iterator in some other portion of the embedded code affect the outcome?
UB is UB. Sometimes it gives false positives. I suppose it depends on what was in s[] on your systems, since it was unitialized. The (un)fortunate observer may have had null characters to break the loop.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*