| Kazekan (66) | |||
|
Hi all, I've been troubleshooting this issue for about 2 hours now and just can't seem to figure out what on Earth is happening. I've narrowed it down to one particular function, but why this function is acting this way is completely beyond me as the same function works fine if I create a new file and copy & paste the necessary code in. The findGCF function is supposed to take a numerator and denominator value and calculate the greatest common factor. Whether or not this function returns a value is not something I'm concerned about, as I mainly want the value of final.gcf to change. This function worked just fine when I first wrote the program without using classes, and worked fine again when rewriting with structs, compiling with either CodeBlocks or g++. For some reason though, it keeps returning the lowest value as the GCF... however, there's a catch. If I put ANY sort of cout statement in that function, it magically starts calculating correctly. The cout statement doesn't even have to reference an actual value - it can be a simple string, and as far as I can tell it doesn't matter where in the function it goes. To add to the weirdness, I created a new cpp file, copied the necessary prototypes and such in and ran the program. No issues at all. I'm at a loss. The code looks exactly the same between the two files, but one is acting like it got hit in the head. Anyway, here's the code for the original file. I've taken out some of the function definitions that are not needed to troubleshoot in order for this to fit:
| |||
|
Last edited on
|
|||
| Kazekan (66) | |||
And, here's the second file I created that works just fine:
Thanks in advance for any help you can give me! | |||
|
Last edited on
|
|||
| ljs (37) | |
| I am not sure if it is the issue here, but the iinteger c in findGCF is not initialized | |
|
|
|
| Kazekan (66) | |
| Yeah, I noticed that yesterday as well but got distracted and didn't follow up on it. I'll try initializing it to 1 and see how it goes. Thanks for the reminder! | |
|
|
|
| ToniAz (341) | |||
|
Hello there friend, you realize that your the piece of code in the first post tests the fraction 53/35, on and on again, right? I added these few lines, just to give the user the chance to set a fraction, and it worked perfectly fine, I'm using Code::Blocks too :D
| |||
|
Last edited on
|
|||
| ToniAz (341) | |||||||
|
Hello there friend, you realize that your the piece of code in the first post tests the fraction 53/35, on and on again, right? I added these few lines, just to give the user the chance to set a fraction, and it worked perfectly fine, I'm using Code::Blocks too :D
ljs is right. Typically, uninitialized ints are very large positive integers. It is better to do this change:
to
| |||||||
|
Last edited on
|
|||||||