#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Somewhere over the Rainbow
    Posts
    128
    Rep Power
    11

    Simple Error with C


    Hi all, At Last I was able to finish my project about Drawing the circle, and when I press some key the circle expand, and when I press other key the circle diminishes , now some errors!.Im using gcc to compile? Error:

    root@root$>gcc circle.c
    circulo.c:38:2: warning: no newline at end of file
    /tmp/cceKFeHK.o(.text+0x64): In function `dist':
    : undefined reference to `sqrt'
    collect2: ld returned 1 exit status



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

    int dist(int ax, int ay, int bx, int by)
    {
    ax-=bx;
    ax=ax<0?-ax:ax;

    ay-=by;
    ay=ay<0?-ay:ay;

    return sqrt((ax*ax)+(ay*ay));
    }

    int main()
    {
    int radius=5;
    int center_x=20, center_y=20;
    int i,j;
    char c=0;

    while(c!='q')
    {
    system("clear");
    for(i=0; i<50; i++)
    {
    for(j=0; j<200; j++)
    if(dist(i,j,center_x,center_y)<radius) printf("*");
    else printf(" ");
    printf("\n");
    }
    c=getchar();
    if(c=='+') radius++;
    if(c=='-') radius--;
    }

    return 0;
    }

    Thanks A lot. :cool:
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11

    Re: Simple Error with C


    Originally posted by Gerardoj
    Hi all, At Last I was able to finish my project about Drawing the circle, and when I press some key the circle expand, and when I press other key the circle diminishes , now some errors!.Im using gcc to compile? Error:

    root@root$>gcc circle.c
    circulo.c:38:2: warning: no newline at end of file
    /tmp/cceKFeHK.o(.text+0x64): In function `dist':
    : undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    For the sqrt problem, you need to include the correct header, which is <math.h>.

    For the newline/end of file warning, try adding another (blank) line after the last closing brace and that may do it (if my memory serves me correctly - a quirk with some versions).
  4. #3
  5. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    11
    That's odd, I could have sworn you didn't include <math.h> but on second glance you did. My bad. You'll need to include <stdlib.h> for system() though.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Gerardoj, did you remember to include the math library?

    gcc circle.c -lm

    (that library option reads "minus ell emm")

IMN logo majestic logo threadwatch logo seochat tools logo