please tell me how to rectify my code

This is a discussion on please tell me how to rectify my code within the C Programming forums, part of the General Programming Boards category; Code: i am not very knowledgeable on structures. i want to input x1,y1 of one point and x2,y2 of other ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    29

    Unhappy please tell me how to rectify my code

    Code:
    i am not very knowledgeable on structures.
    i want to input x1,y1 of one point and x2,y2 of other and get the distance.
    
    this is my code but i am getting error.
    
    #include <stdio.h>
    #include <math.h> 
    
    struct position{
    	double latitude;
    	double longitude;
    };
    
    void main()
    {	
    	double distance;
    	struct position p1,p2;
    	scanf( "%lf, %lf", p1, p2);
    	distance = sqrt( (p1.latitude * p1.longitude) + (p2.latitude * p2.longitude));
    	printf(" %lf", distance);
    }
    Last edited by ilikeapplepie; 12-20-2010 at 06:59 AM. Reason: proper indentation

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    You should be reading into the members of p1 and p2, not directly into p1 and p2.

    Also, post your well indented code within [code][/code] bbcode tags, and use int main instead of void main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    when i read directly into the member, i get an error - undeclared identifier.
    also i need to scanf 4 values. so do i need %lf 4 times inside scanf function?
    thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by ilikeapplepie
    when i read directly into the member, i get an error - undeclared identifier.
    Post your code.

    Quote Originally Posted by ilikeapplepie
    also i need to scanf 4 values. so do i need %lf 4 times inside scanf function?
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    Code:
    #include <stdio.h>
    #include <math.h> 
    
    struct position{
              double latitude;
    	  double longitude;
    };
    
    
    
    int  main(void)
    {	
    	  double distance;
    	  struct position p1,p2;
    	  scanf( "%lf, %lf, %lf, %lf", p1.latitude, p1.longitude, p2.latitude, p2.longitude);
    	  distance = sqrt( (p1.latitude * p1.longitude) + (p2.latitude * p2.longitude));
    	  printf(" %lf", distance);
    }
    Last edited by ilikeapplepie; 12-20-2010 at 06:58 AM. Reason: proper indentation

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    i am sorry i don't know why the indentation disappears.
    i posted my code with the proper indentations.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Looks better now, except that you should have passed pointers with:
    Code:
    scanf("%lf, %lf, %lf, %lf", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);
    Quote Originally Posted by ilikeapplepie
    i am sorry i don't know why the indentation disappears.
    i posted my code with the proper indentations.
    That is why I told you to post within [code][/code] bbcode tags
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by ilikeapplepie View Post
    i am sorry i don't know why the indentation disappears.
    i posted my code with the proper indentations.
    Did you quoted in with code tags?
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    hmm, i added the pointers. now there is no error but i get a weird huge number like

    Code:
    92559631394.......
    Last edited by ilikeapplepie; 12-20-2010 at 07:00 AM.

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Maybe you should pass %f at printf instead of %lf. Maybe...
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    29

    Smile

    Quote Originally Posted by Sipher View Post
    Maybe you should pass %f at printf instead of %lf. Maybe...
    that doesn't help either. i get another i think i should sleep now been awake all night and go through my logic. i think my formula for calculating the distance may be wrong as well. i really appreciate both of yours help. thank you. i will check back later for problem/solution.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    I tested with this program:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct position{
        double latitude;
        double longitude;
    };
    
    int main(void)
    {
        double distance;
        struct position p1, p2;
        scanf("%lf, %lf, %lf, %lf", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);
        distance = sqrt( (p1.latitude * p1.longitude) + (p2.latitude * p2.longitude));
        printf("%f\n", distance);
        return 0;
    }
    Entering 0, 0, 1, 1 gives the expected result of 1.000000.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    latitude is x
    longitude is y

    The formula you're using makes no sense.

    Code:
    distance = sqrt(pow(x1+x2, 2)+pow(y1+y2, 2));
    That's the right formula...
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    Quote Originally Posted by laserlight View Post
    I tested with this program:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct position{
        double latitude;
        double longitude;
    };
    
    int main(void)
    {
        double distance;
        struct position p1, p2;
        scanf("%lf, %lf, %lf, %lf", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);
        distance = sqrt( (p1.latitude * p1.longitude) + (p2.latitude * p2.longitude));
        printf("%f\n", distance);
        return 0;
    }
    Entering 0, 0, 1, 1 gives the expected result of 1.000000.
    i am using visual studio 2008 i rebuilt, debugged and ran my program and i still get the same big number.
    come to think of it, my calculation for the distance was initially wrong.
    Code:
    on x-coordinate P1(0,0) and P2(1,1) should yield 1.414 ( sqrt 2)
    i will post the slightly new code below.

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    29
    this is the slightly different new code.
    Code:
    #include <stdio.h>
    #include <math.h> 
    
    struct position{
    	double latitude;
    	double longitude;
    };
    
    
    
    int  main(void)
    {	
    	double distance_lat;
    	double distance_lon;
    	double distance;
    	struct position p1,p2;
    	scanf( "%f, %f, %f, %f", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);
    	distance_lat=p1.latitude-p2.latitude;
    	distance_lon=p1.longitude-p2.longitude;
    	distance = sqrt( (distance_lat * distance_lat) + (distance_lon * distance_lon));
    	printf(" %f", distance);
    }
    yet i get a big number. i rebuilt, debugged and ran still a big number.
    Last edited by ilikeapplepie; 12-20-2010 at 07:20 AM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21