简体   繁体   English

C程序未打印值

[英]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. 这是我到目前为止所拥有的代码,但是,当我运行它时,问题在于它仅输出decimalDegrees值,而没有其他任何输出。 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). 也许您应该尝试使用其他编译器进行尝试,如果您使用的是基于Unix的系统,则可以尝试使用GCC(如果尚未使用的话),或者尝试使用Xcode(如果使用的是Mac),并且如果运行Windows MinGW是一种流行的选择(适用于Windows的GCC解决方案)。

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() . 正如Paul所建议的,此处不必在printf中使用%lf ,但是如果您打算以后供用户输入自己的值,则可以使用%lf通过scanf()读取双精度数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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