For my code,I need to show the number of how fast sound travels to 4 decimal places, but in the same line after that, I could not get it to show the exact number the user entered. For example, if I enter 205, it will show 205.0000, not just 205 is there any way to fix that? Thanks!
The above means that 4 decimals places will display. The precision setting remains in effect until it is changed to some other value. All you have to do is change the value from 4 to whatever decimal places would you like (or none).
When the fixed manipulator is used, all floating point numbers that are subsequently printed will be displayed in fixed point notation, with the number of digits to the right of the decimal point specified by the setprecision manipulator. When the fixed and setprecision manipulators are used together, the value specified by the setprecision manipulator will be the number of digits to appear after the decimal point, not the number of significant digits. For example, look at the following code.
1 2
double x = 123.4567;
cout << setprecision(2) << fixed << x << endl;
Because the fixed manipulator is used, the setprecision manipulator will cause the number to be displayed with two digits after the decimal point. The value will be displayed as 123.46.
Yes I know that, and that is the requirement. But I want to know how to have the time with setprecision(4) an fixed, but the still displays the distance the way the user enters it, not with the extra decimal places
The precision setting remains in effect until it is changed to some other value. All you have to do is change the value from 4 to whatever decimal places would you like (or none). In other words, you have to change it like this:
I think I understand what you are asking OP. You don't want to be using the "fixed" setting here. Discard that and change your setprecision parameters to (6)
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
enum Roster {Air = 1, Water, Steel};
int medium;
double distance;
constdouble air = 1100,
water = 4900,
steel = 16400;
cout << "This program allows you to find how fast sound travels in different mediums." << endl;
cout << "Through which medium do you want to know?" << endl;
cout << "1. = Air" << endl;
cout << "2. = Water" << endl;
cout << "3. = Steel" << endl;
cin >> medium;
cout << "Enter in the distance it travels in" << endl;
cin >> distance;
if (distance <= 0)
{cout << "Distance must be greater than zero. Please enter a new distance." << endl;
cin >> distance;
}
switch (medium)
{
case Air: cout << "A sound wave takes " << setprecision(6) << distance/air << " seconds to travel " << (static_cast<double>(distance)) << " feet in air." << endl;
break;
case Water: cout << "A sound wave takes " << setprecision(6) << distance/water << " seconds to travel " << distance << " feet in water." << endl;
break;
case Steel: cout << "A sound wave takes " << setprecision(6) << distance/steel << " seconds to travel " << distance << " feet in steel." << endl;
break;
default: cout << "The valid choices are 1 to 3. Run the program again and select one of those." << endl;
}
system("pause");
return 0;
}
@chicofeo, I used an example that was an integer, that would not have worked if the user entered in 205.1, I want to know how to put a code where regardless of what number they entered in, it would show that without being affected by my setprecision(4) earlier in the statement.
@Outlaw782, That didn't work because I need decimal places set to 4 decimal places.
@ OP, I do not have a compiler on this computer to test it, but, I think, If you place cout << setprecision(0) << fixed;
before line 21 (between cin >> medium and cout << "Enter in the distance it travels in" << endl;
However, I don't want to keep giving you the run around without testing it myself.
Try the following, you need to unset your fixed notation, sorry read the question wrong.
1 2 3 4
case Steel: cout << "A sound wave takes " << setprecision(4) << fixed << distance/steel << " seconds to travel " << endl;
cout.unsetf(ios_base::fixed); //unsets your fixed notation
cout << setprecision(10) << distance << " feet in steel." << endl; //set precision(10) now works for up to 10 spaces (including the numbers before the decimal point :D)
break;
Only did this for Steel, will let you do it for air and water
When you set your fixed notation, the program assumed that you wanted to keep a fixed notation for your "distance" output as well. This function "unsets" that fixed notation and returns back to the default notation of double. To be honest, I am not sure if you even need setprecision(10) in there I didn't test that in my compiler. The 'f' is just apart of that function. It doesn't do anything specific it's just apart of the name. The part that you need to be focused on in the function however is the ::fixed which specifies to unset the fixed format flag.