我很好奇为什么如果代码块是一系列代码块中的第一个块,C++为什么不将输出显示为float。让我来解释......拿这个例子:为什么此C++程序不会为第一个输出显示浮点数?
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
double miles;
double fahrenheit;
double gallons;
double pounds;
double inches;
const double milesToKilometers=1.60;
const double fahrenheitToCelsius=5.0/9.0;
const double gallonsToLiters=3.9;
const double poundsToKilograms=0.45;
const double inchesToCentimeters=2.54;
// Get miles, then convert from miles to kilometers.
cout << "Please tell me how many miles you want converted to kilometers:
";
cin >> miles;
if (miles < 0)
cout<<"You cannot enter negative numbers. Please run the program a
again and enter valid number." <<endl<<endl;
else
cout<<miles<<" miles is equal to "<<setprecision(2)
<<fixed<<miles*milesToKilometers<<" kilometers.\n\n";
// Get fahrenheit, then convert from fahrenheit to celsius.
cout << "Please tell me how many degrees fahrenheit you want converted to
celsius: ";
cin >> fahrenheit;
if(fahrenheit < 0)
cout<<"You cannot enter negative numbers. Please run the program again
and enter valid number." <<endl<<endl;
else if(fahrenheit > 1000)
cout<<"You cannot enter numbers above 1000. Please run the program again
and enter valid number." <<endl<<endl;
else
cout<<fahrenheit<<" degree fahrenheit is equal to "<<setprecision(2)
<<fixed<< (fahrenheit-32)*fahrenheitToCelsius <<" celsius.\n\n";
// Get gallons, then convert from gallons to liters.
cout << "Please tell me how many gallons you want converted to liters: ";
cin >> gallons;
if (gallons < 0)
cout<<"You cannot enter negative numbers. Please run the program again
and enter valid number." <<endl<<endl;
else
cout<<gallons<<" gallons is equal to "<<setprecision(2)
<<fixed<<gallons*gallonsToLiters<<" liters.\n\n";
// Get pounds, then convert from pounds to kilograms.
cout << "Please tell me how many pounds you want converted to kilograms: ";
cin >> pounds;
if (pounds < 0)
cout<<"You cannot enter negative numbers. Please run the program again
and enter valid number." <<endl<<endl;
else
cout<<pounds<<" pounds is equal to "<<setprecision(2)
<<fixed<<pounds*poundsToKilograms<<" kilograms.\n\n";
// Get inches, then convert from inches to centimeters.
cout << "Please tell me how many inches you want converted to centimeters:
";
cin >> inches;
if (inches < 0)
cout<<"You cannot enter negative numbers. Please run the program again
and enter valid number." <<endl<<endl;
else
cout<<inches<<" inches is equal to "<<setprecision(2)
<<fixed<<inches*inchesToCentimeters<<" centimeters.\n\n";
return 0;
}
如果你运行它,并输入任何英里数,(例如,10英里),这首码输出将显示:
Please tell me how many miles you want converted to kilometers:10
//input>>10
10 miles is equal to 16.00 kilometers.
的通知输出说10英里不是10.00。现在观察剩余的输出: 如果对于剩余的码块输入10,“华氏摄氏度,”“加仑到升”,“英寸到厘米”的输出将显示10.00,而不是10。另外,如果转代码块的顺序(例如,将'英里转换为公里'代替第一代码块的第二代码块),第一代码块将总是显示一个整数(例如10)而不是浮点数(例如10.00)。
这是怎么回事?
因为您仅为第二个输出设置精确度!!?! – user463035818
因为哟在输出之前从未使用过'setprecision(2)<< fixed' – NathanOliver
我觉得你没有写代码,如果是这样的话请给原作者一个参考 – user463035818