Thread: Need help Vertices program

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    Thumbs down Need help Vertices program

    I need help with my C program that reads the coordinates of the vertices of a polygon from a file?
    and computes the perimeter of the polygon.

    i got the basic program down i just get a weird error message about my distance formula function dist.

    heres my program:

    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
    #include <stdlib.h>

    int main(void)
    {

    typedef struct {double x, y; } vertex_t;
    vertex_t temps;
    FILE *file;
    char flnm [64];
    double perim;
    vertex_t vertexs[20];
    double x; double y;


    printf("Enter the file name: "); /*gets file name*/
    scanf("%s",flnm);

    file = fopen(flnm, "r");

    double temps3;
    int input_status = fscanf(file,"%lf",&temps3);

    perim = 0;
    int i = 0;
    while(input_status != EOF)
    {
    input_status = fscanf(file, "%lf %lf",&x,&y);/*puts the points in the array*/
    temps.x = x;
    temps.y = y;
    vertexs[i] = temps;
    i++;
    }

    for(i=0;i<19;i++)
    {
    perim += dist(vertexs[i], vertexs[i+1]);
    }

    perim += dist(vertexs[0], vertexs[19]);

    printf("The perimeter is %f",perim);

    fclose(file);
    return 1;
    }

    double dist (vertex_t a, vertex_t b)/*gets the distance between two vertices*/
    {

    double distan;

    distan = sqrt(((b.x - a.x)^2) + ((b.y - a.y)^2));
    return distan;

    }


    the error messages i get are:

    [leahyp@pc20 hw8]$ gcc -Wall polygon.c
    polygon.c: In function 'main':
    polygon.c:39: warning: implicit declaration of function 'dist'
    polygon.c: At top level:
    polygon.c:50: error: expected ')' before 'a'


    please help me find whats wrong. i'm pretty sure its my distance function dist but idk what to do to fix it.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The scope of your vertex_t struct (typedef struct {double x, y; } vertex_t is local to the main() function. Move it to just above (outside) your main() function and it should fix your problem.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    omg haha thanks alot that makes complete sense. appreciate it alot!

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    Unhappy

    i moved it outside... but it's still not working :/

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    typedef struct {double x,y;} vertext_t;    <-------
    
    
    int main(void)
    {
    
      vertext_t vertexes[20];
    and now i get a BUNCH of errors and warnings

    Code:
    polygon.c: In function 'main':
    polygon.c:41: warning: implicit declaration of function 'dist'
    polygon.c: At top level:
    polygon.c:52: error: conflicting types for 'dist'
    polygon.c:41: note: previous implicit declaration of 'dist' was here
    polygon.c: In function 'dist':
    polygon.c:57: error: invalid operands to binary ^ (have 'double' and 'int')
    polygon.c:57: error: invalid operands to binary ^ (have 'double' and 'int')

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    ^ is not for exponents in C, it's for XOR'ing. You need to use pow() from math.h.

    You need to declare a prototype for dist() at the top.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    1. You should declare the prototype of dist() after the headers.
    Code:
    double dist (vertex_t a, vertex_t b); //note the semicolon
    2. pow() is the function that computes the power in C, not ^(which is XOR). So, in your dist function, use pow() instead of ^.
    Code:
    pow(x,y) //means x raised to the power y
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    alright i fixed the exponent problem and declared a prototype but i keep getting this weird error:

    Code:
    polygon.c:6: error: expected ')' before 'a'
    polygon.c: In function 'main':
    polygon.c:43: warning: implicit declaration of function 'dist'
    polygon.c: At top level:
    polygon.c:54: error: conflicting types for 'dist'
    polygon.c:43: note: previous implicit declaration of 'dist' was here

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    nevermind got it i just had to switch the order. nevermind

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread