Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Incompatible Pointer Type error while reading from file

 
Reply to this topicStart new topic

Incompatible Pointer Type error while reading from file

vandervander15
25 Sep, 2008 - 07:29 PM
Post #1

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 21

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!!

here it is
CODE


#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define READFILE "points 4.txt"

    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

what does that mean and how do i fix it?

thanks again!
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Incompatible Pointer Type Error While Reading From File
25 Sep, 2008 - 11:16 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,025



Thanked: 35 times
Dream Kudos: 125
My Contributions
in fopen you are supposed to give filename as first parameter.
User is offlineProfile CardPM
+Quote Post

vandervander15
RE: Incompatible Pointer Type Error While Reading From File
26 Sep, 2008 - 06:33 AM
Post #3

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 21

ok so i changes the fopen statement to have "points4.txt" instead of readfile. now it compiles fine BUT it doesnt give any ouputs now

heres the new code

CODE

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define READFILE "points 4.txt"

    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)
Attached File  points4.txt ( 36bytes ) Number of downloads: 3
User is offlineProfile CardPM
+Quote Post

vandervander15
RE: Incompatible Pointer Type Error While Reading From File
26 Sep, 2008 - 06:40 AM
Post #4

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 21

for those who are curious, this is what I get when i hit run

----jGRASP exec: C:\Documents and Settings\Alex\My Documents\Comp 1200\a.exe


----jGRASP cygwin wedge2: process died on signal 11.
----jGRASP: operation complete.


thanks again 4 your help!
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Incompatible Pointer Type Error While Reading From File
26 Sep, 2008 - 06:57 AM
Post #5

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
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.




User is offlineProfile CardPM
+Quote Post

vandervander15
RE: Incompatible Pointer Type Error While Reading From File
26 Sep, 2008 - 08:53 AM
Post #6

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 21

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.

heres my code.
CODE

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define READFILE "points 4.txt"

    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;
   }


thanks again!
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Incompatible Pointer Type Error While Reading From File
26 Sep, 2008 - 09:42 AM
Post #7

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
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.

User is offlineProfile CardPM
+Quote Post

vandervander15
RE: Incompatible Pointer Type Error While Reading From File
26 Sep, 2008 - 10:43 AM
Post #8

New D.I.C Head
*

Joined: 27 Aug, 2008
Posts: 21

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!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:45AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month