setw()

Pretty confused about this. Textbook doesn't cover this in depth and I tried to find other sources but I'm just confusing myself even more...
I'm not looking for answers. Don't give them to me. I just need guidance.
//
Given three variables, a, b, c, of type double that have already been declared and initialized, write some code that prints each of them in a 15 position field on the same line, in such away that scientific (or e-notation or exponential notation) is avoided. Each number should be printed with 5 digits to the right of the decimal point. For example, if their values were 24.014268319, 14309, 0.00937608, the output would be:

|xxxxxxx24.01427xxxx14309.00000xxxxxxxx0.00938

NOTE: The vertical bar, | , on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!

//



My code for this is (which is obviously wrong):
1
2
3
cout << setw(15);
cout << setfill(' ');
cout << fixed << setprecision(5) << a << ' ' << b << ' ' << c;




Please give me some guidance. Thanks!
Last edited on
Your close.

If you read about setw
http://www.cplusplus.com/reference/iomanip/setw
It sets the number of characters to be used as the field width for the next insertion operation.

So you need to specify setw 3 times in your output, once before each double your printing out.

cout << fixed << setprecision(5) << setw(15) << a << ' ' << setw(15) << b << ' ' << setw(15) << c;
Topic archived. No new replies allowed.