我很好奇爲什麼如果代碼塊是一系列代碼塊中的第一個塊,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