2017-06-29 41 views
1

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

這是怎麼回事?

+4

因爲您僅爲第二個輸出設置精確度!!?! – user463035818

+2

因爲喲在輸出之前從未使用過'setprecision(2)<< fixed' – NathanOliver

+1

我覺得你沒有寫代碼,如果是這樣的話請給原作者一個參考 – user463035818

回答

2

缺省值是代表精確的整數值的浮點值被不帶小數點的,沒有任何數字的(未顯示)小數點的右邊顯示。這就是爲什麼第一個是10插入setprecision(2)之後,浮點值是帶小數點和兩個數字在小數點的右邊顯示的所有。這就是爲什麼其餘的是10.00。如果您希望第一個顯示爲10.00,請移動setprecision(2)的插入,以便在顯示第一個值之前完成。

0

你在double聲明的變量;嘗試將它們聲明爲float這樣它將支持小數點,並且您不需要使用setprecision(n)但是!如果你的號碼中的一個回來跟小數點後的數字十億,並要限制隨後的小數,繼續在那裏折騰setprecision(n)回數的量。

相關問題