Questions tagged [iomanip]

Anything related to C++ I/O manipulators, i.e. special kinds of objects that alter the behavior of streams. Inserting a manipulator into an output stream or extracting one from an input stream provides an easy alternative for configuring specific aspects of the stream operations.

Anything related to C++ I/O manipulators, i.e. special kinds of objects that alter the behavior of streams. Inserting a manipulator into an output stream or extracting one from an input stream provides an easy alternative for configuring specific aspects of the stream operations.

Simple manipulators are defined directly in the C++ standard header <iostream>. Manipulators taking arguments (i.e. functions that return actual manipulator objects) are defined in the <iomanip> standard header.

See CPPreference.com documentation for I/O manipulators.

186 questions
7
votes
1 answer

Whats the use of std::cin >> setprecision(n)?

cppreference says: When used in an expression out << setprecision(n) or in >> setprecision(n), sets the precision parameter of the stream out or in to exactly n. The example on the bottom of the same page demonstrates the use with std::cout. Also…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
7
votes
4 answers

Formatting floats: returning to default

I am running into a formatting issue on floating-point values, in the sense of returning to "default formatting". Say I have 2 floats: float f1 = 3.0f, f2 = 1.5f; std::cout << f1 << " - " << f2 << "\n"; will show these as: 3 - 1.5 Now, for some…
kebs
  • 6,387
  • 4
  • 41
  • 70
7
votes
3 answers

is there a good way to combine stream manipulators?

If I wanted to output a fixed width hex number with 4 digits on a stream, I would need to do something like this: cout << "0x" << hex << setw(4) << setfill('0') << 0xABC; which seems a bit long winded. Using a macro helps: #define HEX(n) "0x" <<…
markh44
  • 5,804
  • 5
  • 28
  • 33
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
6
votes
0 answers

Free functions for stream manipulators vs overloading operator<< directly

I'm looking at possible designs for a custom stream class. Half an hour of searching on- and off-site have not led to an answer to my queries, so I'm asking a new question instead. Take std::endl as an example. It is, if I am not mistaken, specified…
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
6
votes
1 answer

Confusion over std::get_money and std::put_money in C++

I am confused with the C++ function std::get_money defined in the header file. What is the use of get_money as per programming concept? I have the following code using std::get_money. #include // std::cin, std::cout #include…
Encipher
  • 1,370
  • 1
  • 14
  • 31
6
votes
1 answer

std::put_money doesn't output zero value when using GCC

When the monetary value is zero, std::put_money doesn't output the '0' character as expected. I can't find anything explaining this behaviour. example: #include #include using namespace std; int main() { …
Jonny Paton
  • 397
  • 2
  • 12
6
votes
1 answer

Read and write using std::hexfloat

This piece of code printed 0 on my machine, but I expected 0.3. What's wrong? I'm using g++ 6.3.1 on latest Arch Linux. Compilation flags seem unrelevent. #include #include int main() { std::stringstream s; s <<…
johnchen902
  • 9,531
  • 1
  • 27
  • 69
6
votes
3 answers

format, iomanip, c++

I'm trying to learn to use namespaces declarations more definitive than not just say "using namespace std". I'm trying to format my data to 2 decimal places, and set the format to be fixed and not scientific. This is my main file: #include…
Crystal
  • 28,460
  • 62
  • 219
  • 393
6
votes
3 answers

Full precision display of floating point numbers in C++?

I have read several topics about the display of floating point numbers display in C++ and I couldn't find a satisfying answer. My question is: how to display all the significant digits of a floating point numbers in C++ in a scientific format…
Vincent
  • 57,703
  • 61
  • 205
  • 388
5
votes
3 answers

std::setprecision sets the number of significant figures. How do I use iomanip to set the precision?

I have always found iomanip confusing and counter intuitive. I need help. A quick internet search finds (https://www.vedantu.com/maths/precision) "We thus consider precision as the maximum number of significant digits after the decimal point in a…
AlastairG
  • 4,119
  • 5
  • 26
  • 41
5
votes
4 answers

How to include two calls of >> in one setw?

Take this a minimal working example #include #include using namespace std; int main() { cout << setw(10) << "aaaaaaa" << setw(10) << "bbbb" << setw(10) << "ccc" << setw(10) << "ddd" …
CroCo
  • 5,531
  • 9
  • 56
  • 88
5
votes
2 answers

How can I make my function sticky for overloaded iostream extraction operators

I'm doing a school project in which I need to change text colour frequently. Project target is Console app currently for Windows only. Using Codeblocks with MinGW for Debugging. I'm not a noob, but Intermediate-ish. So using this everywhere in the…
AneesAhmed777
  • 2,475
  • 2
  • 13
  • 18
5
votes
2 answers

How to use setprecision in cpp for only the number of floating points

When cout-ing a number with floating points, i.e. 31.14159, how can I set cout to use the setprecision(4) on the floating point: cout <
user3639557
  • 4,791
  • 6
  • 30
  • 55
5
votes
1 answer

Resetting output flags in C++

I'm intending to reset all output flags to default on the lines where I end using the resetiosflags function. It provides erroneous output when I attempt to do it in this manner, contrary to my expectations. #include #include…
alxmke
  • 305
  • 1
  • 18
1
2
3
12 13