Originally Posted by
Krak
I removed the function from the header file, and put it into the main source file, and it then worked as it should've....so something in the header file must've been messing things up..but I tried commenting all the functions out and it still worked weird. I suppose it's no big deal anymore though.
But anyhoo...I'm using Dev-C++ on Windows XP Home...and here's that header file that I was making for ........s and giggles.
Here's an experiment (this is how I reproduced your problem)
Make a new project. Put this in the main file of the project:
Code:
#include "z.h" // or whatever your header file is named
int main()
{
float x;
int ix;
float y;
x = 14.45;
y = round(x);
ix = round(x);
cout << "x = " << x << ", y = " << y << ", ix = " << ix << endl;
x = 14.5;
y = round(x);
ix = round(x);
cout << "x = " << x << ", y = " << y << ", ix = " << ix << endl;
cin.get();
return 0;
}
Don't make the header file part of the dev-c++ project.
Now take your original header file and change the round() function to this:
Code:
/************************************************** **
The round function that I was working on until
I stumbled upon something that should not have worked,
but somehow did.
************************************************** ***/
#if 0
float round(float n){
cout << " in round(" << n << ")" << endl;
if ((n / 10)>=0){
n--;
}
return n;
}
#endif
Note that this round() will not be compiled, because of the #if 0 .. #endif
Now press F9 (Execute->Compile & Run)
Here's what I got:
x = 14.45, y = 14
x = 14.5, y = 15
This is the correct result, based on round() in <cmath>
Now: delete the #if 0 and #endif around your float() in the header file. Don't change anything in main(). Execute compile-and-run again.
Here's what I got:
x = 14.45, y = 14
x = 14.5, y = 15
It is not executing your float(), since dev-cpp didn't actually recompile (since the header was not part of the project).
Now, in dev-c++, edit your main (add a space somewhere, or something). Do the compile-and-run thing again. Here's what I got:
in round(14.45)
x = 14.45, y = 13.45
in round(14.5)
x = 14.5, y = 13.5
As expected, now it's running your junky old round() function with the expected wrong answers.
To make sure this doesn't happen again, put add the header file to your dev-c++ project. Now comment out your round() again and compile-and-run. Now it works as expected (at least it does for me).
(I'm not saying that this is what happened, but it does reproduce the results you originally reported.)
Moral of the story: if you think it's doing something that it clearly shouldn't do, then put output statements in strategic places to see if it's really getting there.
An observation: using GUIs to compile projects can give unexpected results if you don't have the project set up correctly. (Same thing with command line compilation with make files.) Fortunately you were testing as you added new functions and recognized that the test results were inconsistent with sane behavior. Testing as you go is a Good Thing.
Of course, "most people" recommend that you not put executable code in header files in the first place, so "most people" would never see this behavior.
Anyhow, I think that it's important to chase down any anomalous behavior as you observe it. Sometimes compilers do have bugs. I don't think that's the case here.
Regards,
Dave