View Full Version : I can't find the error. (Runtime error)
fmucker
12-12-2006, 09:14 PM
Grr. I think I've changed every possible thing and I cannot get this to work. Can someone please help? It is part a of a big project due in 5 hours. When it compiles, I get -1.#QNB for distance. When I use the returned in the rest of my program, it turns all the other values to that as well.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
struct point3d { //self-referential structure for a 3d point
float x;
float y;
float z;
float time;
float distance;
float gravity;
float angle;
float velocity;
struct point3d *nextPtr;
};
typedef struct point3d Point3d; //synonym for struct point3d
typedef Point3d *Point3dPtr;
float determineDistance (Point3dPtr *asteroid);
int main ()
{
Point3d newitem;
Point3dPtr newitemPtr = &newitem;
printf ("\nEnter the initial distance [1.0-10.0]: ");
scanf ("%f", &newitem.x);
printf ("%4.4f %4.4f\n", determineDistance (&newitemPtr), newitem.x);
system ("pause");
}//end main
float determineDistance (Point3dPtr *asteroid) //uses distance formula to determine distance
{
return (sqrt((pow((*asteroid)->x, 2)) + (pow((*asteroid)->y, 2)) + (pow((*asteroid)->z, 2))));
} //end distance formula
Spookster
12-12-2006, 09:40 PM
Is this suppose to be C or C++? What compiler are you using? Your explanation was not clear as far as the error. Are you getting a compiler error or a runtime error? If it's a compiler error then post the error message.
oracleguy
12-12-2006, 09:45 PM
You set the value of x but where do you set the values for y and z?
And if your distance function isn't returning what you expect during runtime, break that math equation up so you can step through and look at each value and see what is not working right. e.g. save the results for each power into its own variable and then add them up and take the square root. Just so you can see where the problem is.
fmucker
12-12-2006, 09:54 PM
Sorry for not being clear. This is a C program, I am using Bloodshed Dev-C++ compiler (v4.9.9.2), and it is a runtime error where it returns the value of -1.#QNB instead of the value it should have. I have tried many variations of the function as well as passing the code and they all return the same. I am hoping for something simple that I didn't think about.
You set the value of x but where do you set the values for y and z?
Maybe I am wrong, but doesn't setting the value of x set all the undefined values to 0? Seems to have always worked like that before.
oracleguy
12-12-2006, 10:01 PM
Maybe I am wrong, but doesn't setting the value of x set all the undefined values to 0? Seems to have always worked like that before.
I typically do C++ instead of C but nonetheless from my experience it shouldn't. The other variables will be uninitialized meaning they could be any value. I'd suspect if you set the y and z to 0 before you call the function, it will start working.
fmucker
12-12-2006, 10:08 PM
I typically do C++ instead of C but nonetheless from my experience it shouldn't. The other variables will be uninitialized meaning they could be any value. I'd suspect if you set the y and z to 0 before you call the function, it will start working.
You sir, are a genious. Thank you very much!!
(BTW, my C book says exactly what I said, but I guess it is wrong.)
Just a quick follow-up...
Maybe I am wrong, but doesn't setting the value of x set all the undefined values to 0?
As far as I'm aware this is completely untrue, as you just found out. You must explicitly define values for each variable.
Why did you get the output you saw?
When you declare a variable, memory is allocated for that variable. However, this memory space still contains the information placed there by whatever program used it last. All that has happened is this space has been labeled usable for your program, the leftover information stays until you overwrite it with new information. So, in your program you were using information in the memory space for y, but that information was leftover from another program since you didn't explicitly tell the program to set y = 0, or some other value.
This is probably the reason that the Java compiler checks that all your variables have been explicitly assigned a value and gives you a compiler error if you try to use a variable that hasn't been initialized.
...some class...
int someVariable;
int x = 0;
x = x + someVariable;
...
The java compiler will give you an error for the above code stating that someVariable "might not have been initialized"
In C (all compilers i've used) it will happily compile and use whatever garbage information is in the memory location for someVariable and continue on, thus giving you interesting results.
Glad you got it working. Feels good doesn't it? :)
fmucker
12-13-2006, 12:09 AM
Glad you got it working. Feels good doesn't it? :)
One little victory doesn't win a war... Yeah, it will feel good when I have this asteroid simulator done. Having problems with the math side of things now... Three hours left to figure it out.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.