我的程序将英尺和英寸转换为米就好了,但它总是会在米和英尺转换之间添加随机数量的英尺。我无法弄清楚为什么。任何建议将不胜感激。 EX)它表示1.3208026m等于8英尺4英寸,实际上是4英尺4英寸。在C++中将仪表转换为英尺和英寸时存在问题
#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;
}
编辑您的问题以添加代码,选中它,然后单击{}按钮以告知编辑器它是代码。无论如何,我怀疑是一个精确问题。 – Robinson
再看看你的while/if循环,具体来说,它的范围是:( –
如果你已经加入了你的调试器,你会看到'脚'增加是否'英寸> 12':( –