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.

119 questions
34
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,742
  • 4
  • 26
  • 41
19
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,625
  • 7
  • 39
  • 52
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
  • 10,837
  • 3
  • 43
  • 67
15
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
  • 249
  • 1
  • 3
  • 13
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
  • 37,899
  • 23
  • 129
  • 288
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
  • 331
  • 1
  • 7
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,790
  • 4
  • 38
  • 48
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
  • 590
  • 1
  • 8
  • 24
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
  • 15,048
  • 4
  • 37
  • 62
5
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
  • 247
  • 2
  • 3
  • 13
5
votes
2 answers

setw not working as expected

I'm trying to print time in hh:mm format but when the time is like 01:01 it prints as 1:1. Here's my code: void output(int hour, int min, char ampm){ cout << setw(2) << setfill('0') << "The time is: " << hour << ":" << min << " "; if(ampm…
granra
  • 318
  • 1
  • 5
  • 17
4
votes
1 answer

setw and accentuated characters

I want to display a "pretty" list of countries and their ISO currency codes on C++. The issue is that my data is in French and it has accentuated characters. That means that Algeria, actually is written "Algérie" and Sweden becomes…
Nuno
  • 117
  • 4
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,990
  • 3
  • 26
  • 37
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
  • 1,093
  • 2
  • 16
  • 30
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
  • 6,176
  • 9
  • 47
  • 77
1
2 3 4 5 6 7 8