Questions tagged [setw]

`std::setw` is a `C++` function used to sets the field width to be used on output operations.

Defined in the <iomanip> include file. See C++ reference page on setw.

104 questions
32
votes
2 answers

"Permanent" std::setw

Is there any way how to set std::setw manipulator (or its function width) permanently? Look at this: #include #include #include #include int main( void ) { int array[] = { 1, 2, 4, 8, 16, 32, 64, 128,…
Miro Kropacek
  • 2,472
  • 2
  • 23
  • 38
17
votes
2 answers

What's the deal with setw()?

I recently was bitten by the fact that ios_base::width and/or the setw manipulator have to be reset with every item written to the stream. That is, you must do this: while(whatever) { mystream << std::setw(2) << myval; } Rather than…
jwd
  • 9,860
  • 3
  • 35
  • 59
17
votes
3 answers

C++ can setw and setfill pad the end of a string?

Is there a way to make setw and setfill pad the end of a string instead of the front? I have a situation where I'm printing something like this. CONSTANT TEXT variablesizeName1 .....:number1 CONSTANT TEXT varsizeName2 ..........:number2 I want…
Dan
  • 2,369
  • 6
  • 36
  • 50
14
votes
3 answers

How do I use iomanip's setw, setfill, and left/right? Setfill isn't stopping its output

I'm trying to get my output to look like this: size time1 time2 ------------------------------- 10 4 8 100 48 16 1000 2937 922 10000 123011 3902 100000 22407380 830722 And…
user3448821
  • 219
  • 1
  • 2
  • 12
9
votes
1 answer

How are iomanip functions Implemented?

Some of the standard iomanip functions take take a parameter. I'd like to know how this is accomplished, for instance, can I do something similar with a function? That's really the solution that I needed for this answer, but I couldn't figure out…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
7
votes
0 answers

std::setw and unicode character

My problem is shown in the following minimal example: #include #include #include int main() { int width = 15; std::cout << std::left; std::cout << std::setw(width) << "Prints well" << std::setw(width) <<…
user119879
  • 71
  • 4
7
votes
1 answer

What is the default `fill character` of std::stringstream?

Is it implementation defined or standards suggest a default fill character for streams? Sample code: #include #include #include int main () { std::stringstream stream; stream << std::setw( 10 ) << 25 <<…
Vikas
  • 8,133
  • 3
  • 35
  • 47
7
votes
4 answers

How to clear width when outputting from a stream, after using std::setw?

I'm using a std::stringstream to parse a fixed format string into values. However the last value to be parsed is not fixed length. To parse such a string I might do: std::stringstream ss("123ABCDEF1And then the rest of the string"); ss >>…
DaBozUK
  • 460
  • 1
  • 7
  • 23
5
votes
1 answer

Reading int with specific field width from stringstream

I'm trying to read bytes in hex notation from a string. The bytes may or may unfortunately not be separated by whitespace, e.g. " 00 ffab c " is a valid example and should result in 4 bytes read, 0x00, 0xff, 0xab and 0x0c. The problem is to skip…
Peter - Reinstate Monica
  • 12,309
  • 2
  • 29
  • 52
4
votes
3 answers

c++ format cout with "right" and setw() for a string and float

I am trying to format a 'cout' where it has to display something like this: Result $ 34.45 The amount ($ 34.45) has to be on right index with certain amount of padding or end at certain column position. I tried using cout << "Result" <<…
ozn
  • 1,390
  • 1
  • 10
  • 27
4
votes
2 answers

C++ setup columns using cout

So i'm just starting to learn c++ and i'm curious if its a way to formate your output with cout so it will look nicely and structured in columns for example. string fname = "testname"; string lname = "123"; double height = 1.6; string…
user3611818
  • 207
  • 1
  • 3
  • 12
4
votes
1 answer

inconsistent behavior of

I have the following code cout << setfill('0') << setw(4) << hex << 100 << 100 << std::endl; The output is: 006464 If I want to let every number with width 4, I have to use out << setfill('0') << setw(4) << hex << 100 << sew(4) << 100 <<…
zhihuifan
  • 879
  • 1
  • 9
  • 22
4
votes
2 answers

c++ console screen size

So I'm learning some stuff in College on C++, and the teacher and I got into a discussion on how to actually center text to the output screen. So my suggestion was to use setw but get the length of the string and the size of the console screen, do…
EasyBB
  • 4,644
  • 7
  • 40
  • 69
3
votes
3 answers

I need something in c# which work like the setw() in c++

I am using a richTextBox in c#. I need to display strings of different length inside one richTextBox but these strings should be perfectly aligned.. this is an example.. abcd abcde abcde ab abc abcdef I know how to do this in c++ using…
Nina
  • 91
  • 3
  • 11
3
votes
1 answer

How does std::setw work with string output?

I am trying to use set width setw for string output to an output file, however, I am not able to make it work. I have with me the following example. // setw example #include #include #include int main () { …
AwaitedOne
  • 932
  • 1
  • 12
  • 36
1
2 3 4 5 6 7