Thread: Anyone see what is wrong with this code?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Question Anyone see what is wrong with this code?

    What's up,

    I'm writing a program in C to calculate the distance between two latitude/longitude coordinates. the below code is that I wrote. It is not working for some reason and I'm clueless to what is wrong. Maby you can catch something that I'm missing. You have any ideas? Any help would be great. Thanks in advance!

    Peace
    Wise1

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    
    main(){
      //Declarations
      double pi=3.14159265358979,a,c,d,dlat,dlon,lat1,lat2,lon1,lon2;
      int days,hours,s,r=3956;
    
      //Header
      printf("Origin Coordinates \n  1) 16, 64 \t\t Speed: 20 mph \n  2) 32, 64 (Bermuda) \t Speed: 10 mph \n\nDestination Coordinates \n  1) 26, 80 (Ft. Lauderdale) \n\n\n");
    
      //Read Input
      printf("Please Enter the Origin Coordinates: ");
      scanf("%f, %f",&lat1,&lon1);
      printf("Please Enter the Destination Coordinates: ");
      scanf("%f, %f",&lat2,&lon2);
      printf("Please Enter the Speed of Travel: ");
      scanf("%d",&s);
    
      //Convert Coordinates From Decimal Degrees to Radians
      lat1*=pi/180;
      lat2*=pi/180;
      lon1*=pi/180;
      lon2*=pi/180;
    
      //The Haversine Formula
      dlat=lat2-lat1;
      dlon=lon2-lon1;
      a=((sin(dlat/2))*(sin(dlat/2)))+cos(lat1)*cos(lat2)*((sin(dlon/2))*(sin(dlon/2)));
      c=2*atan2(sqrt(a),sqrt(1-a));
      d=r*c;
    
      //Find Time
      hours=d/s;
      days=hours/24;
      hours-=days*24;
    
      //Print Output
      printf("\n\nDistance: %f Statute Miles \nSpeed: %d mph\nTime (Days/Hours): %d/%d",d,s,days,hours);
    
      getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > double pi=3. 14159265358979
    1. it has a space
    2. use M_PI in math.h (which you already include)

    > scanf("%f, %f",&lat1,&lon1);
    You're using the wrong conversion for reading into doubles

    scanf("%lf, %lf",&lat1,&lon1);

    > It is not working for some reason and I'm clueless to what is wrong
    Assuming that the input was correct was the first mistake
    The second was not trying the obvious step of printing them straight back to the user for verification

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    2
    What's up Salem,

    Yep that was it. Thanks!

    Peace
    Wise1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. Replies: 4
    Last Post: 12-07-2002, 04:24 PM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM