简体   繁体   中英

C program not printing values

I am writing a program that converts decimal degrees to degrees, minutes, seconds. This is what it is supposed to do:

-118.1406209154 = 118 degrees 8 minutes 26.2342 seconds West.
W     If it is negative then it is either west longitude or south latitude
118     The degrees are left of the decimal place
8     Multiply the decimal portion (0.140620915) by 60 to get decimal minutes (8.4372549). The minutes are left of the decimal place.
26.2353     Multiply the decimal portion (0.4372549) by 60 to get decimal seconds.

This is the code I have so far, however, the problem when I run it is it only prints the decimalDegrees value and nothing else. I cannot figure out why. Any tip/help is much appreciated.

double decimalDegrees = -118.1406209154;

double degrees;
double minutes;
double seconds;
char longitude;
char latitude;
double temp;

int main(int argc, char **argv)
{
    minutes = decimalDegrees - (int)decimalDegrees; //get the decimal portion of decimalDegrees
    degrees = decimalDegrees - minutes; //get degrees by removing the decimal portion
    minutes = minutes*60.0;
    temp = minutes - (int)minutes; //get decimal portion
    minutes = minutes - temp; //remove decimal portion from minutes
    seconds = temp * 60.0;

    printf("%0.10lf ", decimalDegrees);
    printf("%lf degrees ", degrees);
    printf("%lf minutes ", minutes);
    printf("%0.4lf seconds ", seconds);

    return 0;
}

This works for me fine, so far. Perhaps you should attempt this with a different compiler, if you are using a unix based system, you could try using GCC if you haven't already, or Xcode if you are using a Mac, and if running windows MinGW has been a popular choice (a GCC solution for windows).

Taking into account what others have already said, you could better your code a little while you are at it. As Paul suggested, the use of %lf is unnecessary to use in printf here, but if you intend for the user to enter their own values in later on, you will use %lf for reading in doubles using scanf() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM