-
setiosflags(ios::fixed)
I'v used this flag successfully before, but for some reason it won't format the numbers in this code. They keep coming back in scientific notation no matter where I set the flags. Does anyone know what I'm doing wrong? Here's the code:
double GetSquare(double x)
{
double a;
a = (sqrt(x));
return a;
}
int main()
{
double x;
cin >> x;
cout << setiosflags(ios::fixed) << setprecision(0)
<< endl << "The square root of "<< x << " is " << GetSquare(x)
<< endl <<endl;
return 0;
}
-
cout << setiosflags(ios::fixed) << setprecision(0)
<< endl << "The square root of "<< x << " is " << GetSquare(x)
<< endl <<endl;
by saying set precision (0) you have left no room for the output. so the system will always try and print and make the room it needs. if there is not enough room it will automatically print in scientific notation.
in this case, whenever you say ios::fixed *along with* setprecision() it means that you are telling it how many places after the decimal place you want printed.
setprecision( #) without the ios::fixed means how many places you want for the entire number.
it was confusing to me that it means 2 different things depending on if you choose ::fixed or not.
try changing the zero to a bigger number.
susan