为什么这个C ++程序没有为第一个输出显示浮点数?

时间:2017-06-29 16:44:17

标签: c++

我很好奇为什么如果代码块是一系列代码块中的第一个块,C ++不会将输出显示为float。让我解释一下......举个例子:

#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,那么输入华氏度到摄氏度,&#39; &#39; Gallons to Liters&#39;,Inches to Centimeters&#39;输出将显示10.00,而不是10.此外,如果您转换代码块的顺序,(例如,使英里到公里&#39;第二个代码块而不是第一个代码块),第一个代码块将始终显示一个int(例如10)而不是float(例如10.00)。

这是什么?

2 个答案:

答案 0 :(得分:2)

默认情况下,表示精确整数值的浮点值显示时没有小数点,并且在(未显示的)小数点右侧没有任何数字。这就是为什么第一个是10 插入setprecision(2)后,浮动值全部显示小数点,小数点右侧有两位数。这就是为什么其余的都是10.00。如果您希望第一个显示为10.00,请移动setprecision(2)的插入,以便在显示第一个值之前完成。

答案 1 :(得分:0)

您在double处声明了变量;尝试将它们声明为float,这样它将从一开始就支持小数,您将无需使用setprecision(n)但是!如果您的某个号码在小数点后返回十亿个数字,并且您想限制小数点后面的数字量,请继续将setprecision(n)放回去。