视觉 在 c++ 中将米转换为英尺和英寸时出现问题

visual Having an issue with meter conversion to feet and inches in c++

本文关键字:问题 c++ 转换 视觉      更新时间:2023-10-16

我的程序可以将英尺和英寸转换为米,但它总是会在米上随机添加英尺和英寸的转换量。我不知道为什么。任何建议将不胜感激。EX) 它指出 1.3208026m 等于 8 英尺 4 英寸,而实际上它是 4 英尺 4 英寸。

#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;

int feet;
double meters, inches;
int main()
{
        cout << "Enter feet " << endl;
        cin >> feet;
        cout << "Enter inches " << endl;
        cin >> inches;
        inches = feet * 12 + inches;
        meters = inches / 39.370;
        cout << setprecision(8) << meters << "m" << endl << endl;
        cout << "Enter meters. " << endl;
        cin >> meters;
        inches = meters * 39.37;
        while (inches > 12)
        {
            if (inches > 12)
                inches = inches - 12;
            feet++;
        }
    cout << feet << " ft and " << setprecision(8) << inches << "in" << endl;
    return 0;
}

您需要在第一次转换后将英尺重置回零。

    if (inches > 12)
        inches = inches - 12;
    feet++;

不。 需要牙套,否则你的脚会让你失望。