简体   繁体   中英

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

I'm curious as to why C++ does not display an output as a float if the code block is the first block in a series of code blocks. Let me explain...Take this example:

#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;
}

If you run this, and enter any number of miles, (eg 10 miles) this first code output will display:

Please tell me how many miles you want converted to kilometers:10
//input>>10
10 miles is equal to 16.00 kilometers.

Notice the output says 10 miles not 10.00. Now observe the remaining outputs: if you enter 10 for the remaining code blocks, 'fahrenheit to Celsius,' ' Gallons to Liters', 'Inches to Centimeters,' the output will display 10.00, not 10. In addition, if you transpose the order of code blocks, (eg make 'miles to kilometers' the second code block instead of the first code block), the first code block will always display an int (eg 10) instead of a float (eg 10.00).

What's up with this?

The default is that floating-point values that represent exact integer values are displayed without a decimal point and without any digits to the right of the (not-displayed) decimal point. That's why the first one is 10 . After inserting setprecision(2) , floating-point values are all displayed with a decimal point and two digits to the right of the decimal point. That's why the rest are 10.00 . If you want the first one to be shown as 10.00 , move the insertion of setprecision(2) so that it's done before displaying the first value.

You're declaring your variables at double ; try declaring them as a float that way it will support decimals from the get-go and you'll have no need to use setprecision(n) . But! If one of your numbers comes back with a billion numbers after the decimal and you want to limit the amount of numbers that follow the decimal, go ahead and toss setprecision(n) back in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM