Join 136,583 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,948 people online right now. Registration is fast and FREE... Join Now!
ok so i need help with this error code. its a program that reads data points from a file and calculates the distance in between successive points and then find displacement - distance from origin to the endpoint. i think theres something wrong with the read from the file. plz help!!
int main() { //Inputs int x; int y; int count; // Outputs int distanceTraveled; int displacement; int difference; // Intermediate Values double x0; double y0; double distance; double c = 0;
FILE *readfile; // Use pointer and fopen to open file readfile = fopen( readfile, "r");
if ( readfile == NULL ) printf("File Open ERROR \n"); //check for file validity else //run program { fscanf( readfile, "%d", count); // Get number of points in file printf("Distance Traveled VS Displacement\n"); printf(" Point 1 Point 2 Distance\n");
while (c < count) { fscanf( readfile, "%d %d", &x, &y); // Read X and Y distance = sqrt (((x-x0)*(x-x0)) + ((y-y0)*(y-y0))); // Calculate distance between points printf("( %.1lf, %.1lf ) ( %.1lf, %.1lf ) %.2lf\n", x0, y0, x, y, distance); // display distance between two points and those 2 points x = x0; // store x as x0 y = y0; // store y as y0 distanceTraveled += distance; // calculate total distance traveled c += 1; } } displacement = sqrt((x0*x0) + (y0*y0)); // calculate displacement
difference = distanceTraveled - displacement; // calculate difference //Display total distance traveled, displacement, and difference printf("Total Distance Traveled %.2lf\n", distanceTraveled); printf("Direct Distance from ( 0 , 0 ) to ( %.1lf , %.1lf ) is %.2f\n", x0, y0, displacement); printf("\nThe difference between total distance traveled and displacement is %.2lf.\n", difference); return 0; }
i get this error message
warning: passing arg 1 of `fopen' from incompatible pointer type
int main() { //Inputs int x; int y; int count; // Outputs int distanceTraveled; int displacement; int difference; // Intermediate Values double x0; double y0; double distance; double c = 0;
FILE *readfile; // Use pointer and fopen to open file readfile = fopen( "points4.txt", "r");
if ( readfile == NULL ) printf("File Open ERROR \n"); //check for file validity else //run program { fscanf( readfile, "%d", count); // Get number of points in file printf("Distance Traveled VS Displacement\n"); printf(" Point 1 Point 2 Distance\n");
while (c < count) { fscanf( readfile, "%d %d", &x, &y); // Read X and Y distance = sqrt (((x-x0)*(x-x0)) + ((y-y0)*(y-y0))); // Calculate distance between points printf("( %.1lf, %.1lf ) ( %.1lf, %.1lf ) %.2lf\n", x0, y0, x, y, distance); // display distance between two points and those 2 points x = x0; // store x as x0 y = y0; // store y as y0 distanceTraveled += distance; // calculate total distance traveled c += 1; } } displacement = sqrt((x0*x0) + (y0*y0)); // calculate displacement
difference = distanceTraveled - displacement; // calculate difference //Display total distance traveled, displacement, and difference printf("Total Distance Traveled %.2lf\n", distanceTraveled); printf("Direct Distance from ( 0 , 0 ) to ( %.1lf , %.1lf ) is %.2f\n", x0, y0, displacement); printf("\nThe difference between total distance traveled and displacement is %.2lf.\n", difference); return 0; }
It gives no outputs when run but still says that it did run the program :-/
I attached the points4.txt to this post
Attached File(s) points4.txt ( 36bytes )
Number of downloads: 3
First, you need to compile with all warnings on, and some of this will become apparent. Try gcc -g -Wall -o a.exe -lm prog.c
Next, and what's causing your crash, you need to pass the address of count here:
cpp
fscanf( readfile, "%d", &count);
Next up, you should default all your arguments before you use them. You use some variables before they've been assigned anything, so they could contain anything. It may not be an issue here, but it's a good habit to get into.
Next, I don't think this code matches up with your comments:
cpp
x = x0; // store x as x0 y = y0; // store y as y0
If you don't reverse that there, x0 and y0 are never going to be assigned.
Those points should get you headed in the right direction.
wow that helped a ton. after reversing and defaulting the variables i get some numbers other than 0. now its just crazy numbers so i dont think that it is reading from the file correctly. also, about the compiling operation, how do I do that? all my teacher has taught us is how to click the compile and run buttons and check for parse errors. I have looked at this code for an hour or so and all I can come up with with my limited knowledge is that it isnt reading from the file correctly. it looks right to me though so i dont know. thanks for all the help.
int main() { //Inputs int x; int y; int count; // Outputs int distanceTraveled; int displacement; int difference; // Intermediate Values double x0 = 0; double y0 = 0; double distance = 0; double c = 0;
FILE *readfile; // Use pointer and fopen to open file readfile = fopen( "points4.txt", "r");
if ( readfile == NULL ) printf("File Open ERROR \n"); //check for file validity else //run program { fscanf( readfile, "%d", &count); // Get number of points in file printf("Distance Traveled VS Displacement\n"); printf(" Point 1 Point 2 Distance\n");
while (c < count) { fscanf( readfile, " %d %d ", &x, &y); // Read X and Y distance = sqrt (((x-x0)*(x-x0)) + ((y-y0)*(y-y0))); // Calculate distance between points printf("( %.1d, %.1d ) ( %.1d, %.1d ) %.2lf\n", x0, y0, x, y, distance); // display distance between two points and those 2 points x0 = x; // store x as x0 y0 = y; // store y as y0 distanceTraveled += distance; // calculate total distance traveled c += 1; } } displacement = sqrt((x0*x0) + (y0*y0)); // calculate displacement
difference = distanceTraveled - displacement; // calculate difference //Display total distance traveled, displacement, and difference printf("Total Distance Traveled %.2lf\n", distanceTraveled); printf("Direct Distance from ( 0 , 0 ) to ( %.1d , %.1d ) is %.2d\n", x0, y0, displacement); printf("\nThe difference between total distance traveled and displacement is %.2d.\n", difference); return 0; }
How you get the warnings is going to depend on the IDE you're developing with.
You're printing integers as doubles and vice-versa in some of your printf statements. Here are the warnings I get:
CODE
dist2.c: In function ‘main’: dist2.c:36: warning: format ‘%.1d’ expects type ‘int’, but argument 2 has type ‘double’ dist2.c:36: warning: format ‘%.1d’ expects type ‘int’, but argument 3 has type ‘double’ dist2.c:47: warning: format ‘%.2lf’ expects type ‘double’, but argument 2 has type ‘int’ dist2.c:48: warning: format ‘%.1d’ expects type ‘int’, but argument 2 has type ‘double’ dist2.c:48: warning: format ‘%.1d’ expects type ‘int’, but argument 3 has type ‘double’
Once I fix everything here, this is the output I get:
CODE
Distance Traveled VS Displacement Point 1 Point 2 Distance ( 0.0, -0.0 ) ( 2, 1 ) 2.24 ( 2.0, 1.0 ) ( 3, 5 ) 4.12 ( 3.0, 5.0 ) ( 7, 3 ) 4.47 ( 7.0, 3.0 ) ( 10, 13 ) 10.44 ( 10.0, 13.0 ) ( 14, 11 ) 4.47 Total Distance Traveled 24 Direct Distance from ( 0 , 0 ) to ( 14.0 , 11.0 ) is 17
The difference between total distance traveled and displacement is 7.
THANK YOU SO MUCH! i am definitely gonna have 2 figure out how 2 get the compile warnings fixed in my IDE. shouldnt be 2 hard right? lol. THank you so much JackOfAllTrades!