Thread: a tiny problem with a function

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    Question a tiny problem with a function

    hello,

    I'm new with c programming and...

    I'm trying to program a function which should perform the mathematical ditstance formula

    but I 'm getting this error: found '{' at file scope (missing function header?

    this is the code that I wrote

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float distance(float x1, float x2, float y1, float y2);
    {
    	float a=0 ,b=0 ,c=0, d=0 e=0;
    	a= x2 -x1;
    	b= y2 -y1;
    	c = pow(a,2);
    	d = pow(b,2);
    
    	c+d=e
    
    
    
    	return sqrt(e);
    	
    
    
    }
    
    int main()
    
    {
    	x1=2; x2=4; y1=3; y2=6;
    
    	printf("%lf\n",distance(x1,x2,y1,y2);
    
    	return 0;
    }
    what do I do wrong?

    thenks for helping..

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    float distance(float x1, float x2, float y1, float y2);  //semicolon does not belong here, it thinks you're making a function prototype
    {
    	float a=0 ,b=0 ,c=0, d=0 e=0;
    	a= x2 -x1;
    	b= y2 -y1;
    	c = pow(a,2);
    	d = pow(b,2);
    
    	c+d=e
    
    
    
    	return sqrt(e);
    	
    
    
    }

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    printf("%lf\n",distance(x1,x2,y1,y2);
    missing a parenthesis there.

  4. #4
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    c+d=e
    pretty sure you want to do e=c+d

    Also, you don't need to initialize all the local floats in distance() to 0, and you don't really need to use pow(,), you could just say c=a*a; and d=b*b;

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sing0 View Post
    hello,

    I'm new with c programming and...

    I'm trying to program a function which should perform the mathematical ditstance formula

    but I 'm getting this error: found '{' at file scope (missing function header?

    this is the code that I wrote

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float distance(float x1, float x2, float y1, float y2);
    {
    	float a=0 ,b=0 ,c=0, d=0 e=0;
    	a= x2 -x1;
    	b= y2 -y1;
    	c = pow(a,2);
    	d = pow(b,2);
    
    	c+d=e
    
    
    
    	return sqrt(e);
    	
    
    
    }
    
    int main()
    
    {
    	x1=2; x2=4; y1=3; y2=6;
    
    	printf("%lf\n",distance(x1,x2,y1,y2));
    
    	return 0;
    }
    what do I do wrong?

    thenks for helping..
    Other error is in main, printf doesn't have a ending bracket.
    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

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    I removed it( the semicolon)
    and got lots of error

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Code:
    	c+d=e
    This line should probably end with a ;

  8. #8
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    x1=2; x2=4; y1=3; y2=6;
    you need to declare these as floats so that they'll work with your function and so they just have a type.
    Code:
    float x1=2, x2=4, y1=3, y2=6;

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by sing0 View Post
    I removed it( the semicolon)
    and got lots of error
    because it's actually parsing through the rest of the function now

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by sing0 View Post
    hello,

    I'm new with c programming and...

    I'm trying to program a function which should perform the mathematical ditstance formula

    but I 'm getting this error: found '{' at file scope (missing function header?

    this is the code that I wrote

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float distance(float x1, float x2, float y1, float y2);
    {
    	float a=0 ,b=0 ,c=0, d=0 e=0;
    	a= x2 -x1;
    	b= y2 -y1;
    	c = pow(a,2);
    	d = pow(b,2);
    
    	c+d=e
    
    
    
    	return sqrt(e);
    	
    
    
    }
    
    int main()
    
    {
    	x1=2; x2=4; y1=3; y2=6;
    
    	printf("%lf\n",distance(x1,x2,y1,y2);
    
    	return 0;
    }
    what do I do wrong?

    thenks for helping..

    So many basic problems in your code

    dude so many i count so many

    1.....
    Code:
    float distance(float x1, float x2, float y1, float y2); // there is no need of ; here remove it
    2.....
    Code:
    float a=0 ,b=0 ,c=0, d=0 e=0; // there is a need of , between d and e
    3.....
    Code:
    c+d=e // dont know what you want to do it here may be you are doing e = c + d
    e = c + d;

    4.....
    Code:
    c+d=e // need ; after the statement
    e = c + d;
    5.....
    Code:
    float x1=2; x2=4; y1=3; y2=6; // here you need coma instead of semicolon
    float x1=2, x2=4, y1=3, y2=6; // here you need coma instead of semicolon

    6.....
    Code:
     printf("%f\n",distance(x1,x2,y1,y2); // missing ) parenthesis 
     printf("%f\n",distance(x1,x2,y1,y2));


  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    thank u for the quick help

    I fixed all the problems u told me but I still getting this error:
    Code:
    1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(5) : error C2449: found '{' at file scope (missing function header?)
    1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(19) : error C2059: syntax error : '}'

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Show us the modified code.
    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

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sing0
    I fixed all the problems u told me but I still getting this error:
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    Talking

    Quote Originally Posted by Epy View Post
    because it's actually parsing through the rest of the function now
    a good one

  15. #15
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by sing0 View Post
    thank u for the quick help

    I fixed all the problems u told me but I still getting this error:
    Code:
    1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(5) : error C2449: found '{' at file scope (missing function header?)
    1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(19) : error C2059: syntax error : '}'
    If your code is same which you posted first than you can refer to the solution provided by my fellow forum member's and if you changed the code than please post the code here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM