2017-06-29 42 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)回数的量。

相关问题