setiosflags(ios::fixed)

This is a discussion on setiosflags(ios::fixed) within the C++ Programming forums, part of the General Programming Boards category; I'v used this flag successfully before, but for some reason it won't format the numbers in this code. They keep ...

  1. #1
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Angry 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;
    }
    "The mind, like a parachute, only functions when open."

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    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

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21