| cPlusN00b (92) | |||
|
Hey guys this is really messing with my head. Thanks in advance for any help yall can give me. I am trying to write a simple number sequence type program for someone.. this code compiles and runs... but for some reason, I have no idea why, the program just seems to get stuck between two lines of code. I used stuff like: if (x==113383){cout << "Line 00";}to find out where it was going wrong, and I've inserted a comment into the code below. The line which preceeds the comment runs just fine, but the line after the comment apparently never happens when (x == 113383)! So anyway here's the code. Go ahead and run it, please:
What goes on here? I am so lost.. is this an unusual problem? Thanks for any suggestions. - cPlusN()()b p.s. I have used long int too, makes no difference, tho I didn't expect it to.. the statement: w = (x % 2);runs, then, nothing. :/ | |||
|
Last edited on
|
|||
| cPlusN00b (92) | |
|
UPDATE ok so I used unsigned int for quantum and x and now it works perfectly. I think. Can anyone please explain whats going on here? after all 1000000 is much more than 2 * 113383, and the value of x should never be negative, so why is the signed/unsigned causing trouble? I'm scared. | |
|
Last edited on
|
|
| cire (2354) | ||||
This is one of the Euler problems. Have someone write his own code.
Unfortunately, no. Not that unusual. Don't use signed values where unsigned values are called for. The condition you should've been looking for on line 32 is x < 0.
long int and int hold the same range of values on your system. long long would've worked. Or.. just an unsigned int. Btw, your count will be off by one. | ||||
|
|
||||
| cPlusN00b (92) | |
for (int quantum=1; quantum < 1000001; quantum++) that better? lol nevermind, I think I see what ya meant thanks for helping me with this, cire | |
|
Last edited on
|
|
| EssGeEich (1009) | |||
|
Because when you add too much to a variable, this variable will overflow resulting in negative values. Try a test like:
| |||
|
|
|||
| greenleaf800073 (84) | |
| I just ran some debugging, and i'm so close to fixing it. When it reaches 113383 or near it, w starts to go negative...... | |
|
|
|
| greenleaf800073 (84) | |
| I think, 113383 is a special number, and it runs your program forever and then your program crashes and freezes... | |
|
|
|
| cPlusN00b (92) | |
|
already fixed the problem green ^ I made "quantum" and "x" unsigned int thanks for all the help peoples | |
|
Last edited on
|
|
| greenleaf800073 (84) | |
| Wait, how did that help? Sorry, I don't use those terms much. | |
|
|
|