簡體   English   中英

為什么此C ++程序不為第一個輸出顯示浮點數?

[英]Why does this C++ program not display a float for the first output?

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

這是怎么回事?

缺省情況是,表示精確整數值的浮點值顯示時不帶小數點,並且在(未顯示)小數點的右邊沒有任何數字。 這就是為什么第一個是10的原因。 插入 setprecision(2)浮點值與一個小數點和兩個數字在小數點右側的顯示。 這就是為什么其余為10.00的原因。 如果希望第一個顯示為10.00 ,請移動setprecision(2)的插入,使其在顯示第一個值之前完成。

您要double聲明變量; 嘗試將它們聲明為float ,這樣它將一開始就支持小數,並且您無需使用setprecision(n) 但! 如果您的一個數字在小數點后返回十億個數字,並且您想限制小數點后的數字量,請繼續並將setprecision(n)放回去。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM