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
5
votes
1 answer

C++ cout decimal alignment

I'm having a hard time aligning decimal values. I am pretty sure its a combination of right alignment and setprecision/fixed but it doesn't seem to be working. I know other questions have been asked on the topic but I haven't found a clear solution…
YelizavetaYR
  • 1,611
  • 6
  • 21
  • 37
5
votes
3 answers

Really, what's the opposite of "fixed" I/O manipulator?

This may be a duplicate of this question, but I don't feel it was actually answered correctly. Observe: #include #include using namespace std; int main () { float p = 1.00; cout << showpoint << setprecision(3) << p <<…
rmp251
  • 5,018
  • 4
  • 34
  • 46
4
votes
0 answers

using stream operator<< with std::endl in c++

I am trying out the following C++ class for using the stream operator << to log contents from this answer: class Log { public: Log() : m_filename( "dafault.log" ) {} // if you wanna give other names eventually... Log( const…
thor
  • 21,418
  • 31
  • 87
  • 173
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

Send formatted char array to ostream without extra memory copying

I need to send a char array to ostream. Say I have the following print function: Version 1: void print(ostream &out, const char *str, unsigned len) { out << string(str,len); } Version 2: void print(ostream &out, const char *str, unsigned…
4
votes
2 answers

Why use showpoint when you can use setprecision fixed?

I don't quite understand the purpose of showpoint, i know it forces to show a decimal point, but having "cout << setprecision << fixed" is enough without the use of showpoint. Can you show me an example where showpoint is a must have?
user97662
  • 942
  • 1
  • 10
  • 29
4
votes
2 answers

How can I increase the number of digits displayed when using Armadillo library

I am using the Armadillo linear algebra library to diagonalize matrices. I need to increase the number of digits displayed/written to a file at the end. According to Armadillo's reference, "arma::mat" will create a double matrix. So, I tried using…
S.G.
  • 357
  • 2
  • 15
4
votes
1 answer

Display numbers with padding and a fixed number of digits in C++

I'd like to display numbers using a padding (if necessary) and a fixed number of digits. For instance, given the following numbers: 48.3 0.3485 5.2 Display them like this: 48.30 00.35 05.20 I'm trying combinations of std::fixed, std::fill,…
aardvarkk
  • 14,955
  • 7
  • 67
  • 96
4
votes
3 answers

Set the number of digits for an Integer

Is there a way in C++ to make the compiler take a certain number of digits even if they first digits are 0's. For example: I have an item number that is 00001 and when I import the number from the file it displays a 1. I want it to import all five…
Alex McCoy
  • 91
  • 1
  • 2
  • 4
4
votes
6 answers

How to print a bunch of integers with the same formatting?

I would like to print a bunch of integers on 2 fields with '0' as fill character. I can do it but it leads to code duplication. How should I change the code so that the code duplication can be factored out? #include #include…
Ali
  • 56,466
  • 29
  • 168
  • 265
3
votes
1 answer

Using iomanip to format data output to a text file with Qt

I am a student programmer using QT to develop and application for work. Currently I am developing the save functions in which data is taken from a table and saved to a file. Im running into some trouble when I try to write the data into columns. Not…
Wylie Coyote SG.
  • 1,009
  • 6
  • 22
  • 37
3
votes
1 answer

Can std::hex input format support negtive int16_t hex string with two's complement notation such as `ffff` for `-1`?

I would like to input a text string ffff to a int16_t, the value should be -1. Here is a simple test C++ program: #include #include #include int main() { int16_t num; std::cout << "Enter a hexadecimal…
ollydbg23
  • 1,124
  • 1
  • 12
  • 38
3
votes
2 answers

Why do you need to use both fixed and showpoint manipulators to show decimal point and trailing zeros when only fixed does the job?

I understand what the fixed and showpoint manipulators do on their own, but why would you want to use them both at the same time? This is what DS Malik's C++ Programming textbook says: Of course, the following statement sets the output of a…
ewzuhnaem
  • 33
  • 1
  • 5
3
votes
1 answer

Find current base of output stream

Is it possible, when overloading the << operator to write to an output stream, to get what numeric base this stream is currently in? E.g. if std::hex was called before calling my overloaded operator, can I find out if the stream is currently in "hex…
Lauchmelder
  • 130
  • 8
3
votes
1 answer

How to align user input in C++ (not using iomanip)

My program is supposed to align inputted text based on what the user specifies, so far I've gotten it to change the width but not align the text (Left, Right, Center). I've seen but it doesn't help in my case. The code I've gotten so far…
Roy Pugh
  • 31
  • 3
1 2
3
12 13