ubuntu系统下C++变量定义问题

在C++ primer plus计算经纬度的练习题中,

img


我使用一下代码进行计算

#include <iostream>

int main(){
    using namespace std;
    cout << "Enter a latitude in degrees, minutes and seconds\n";
    int degree;
    cout << "enter a degree:";
    cin >> degree;
    int minutes;
    cout << "enter a minutes:";
    cin >> minutes;
    int seconds;
    cout << "enter a seconds:";
    cin >> seconds;
    // const int trans = 60;
    double degree_print = degree + minutes/60  + seconds/3600;
    cout << degree << " degrees " << minutes << " minutes " << seconds << " seconds " << "= " << degree_print << endl;
    return 0;
}


但最终的计算结果并不正确,而是和我定义的degree保持一致,请问如何处理?感谢。

Enter a latitude in degrees, minutes and seconds
enter a degree:23
enter a minutes:23
enter a seconds:23
23 degrees 23 minutes 23 seconds =23
[1] + Done                       "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-340vt1ac.hjh" 1>"/tmp/Microsoft-MIEngine-Out-o5posge2.ttl"

double degree_print = degree + minutes/60.0 + seconds/3600.0;
由于minutes和seconds都是int类型,两个整数相除是整除,所以要改为60.0和3600.0,否则后面两个除法结果都是0

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^