How to ouput an integer with positive sign and zeros preceding it - c++

I want a number to be displayed with a positive sign and three 0's preceding it, but what I am getting so far is 000+1 when what I want is +0001
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
int number = 1;
cout << showpos;
cout << setfill('0') << setw(5) << number << endl;
}

You need to also set std::internal flag. This way you will get your expected +0001 - test at ideone.

This is what the std::internal manipulator is for. For example,
std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << std::endl;
prints "-0005" instead of "000-5" as without std::internal.

Related

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" << setw(15) << right << "$ " << 34.45" << endl;
However, it's setting the width for the "$ " string, not for the string plus amount.
Any advice on dealing with such formatting?
You need to combine "$ " and value 34.45 into separate string. Try like this:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
stringstream ss;
ss << "$ " << 34.45;
cout << "Result" << setw(15) << right << ss.str() << endl;
}
You try to apply a format modifier to two arguments of different types (string literal and a double), which can't work out. To set a width for both the "$ " and the number, you need to convert both to a string first. One way would be
std::ostringstream os;
os << "$ " << 34.45;
const std::string moneyStr = os.str();
std::cout << "Result" << std::setw(15) << std::right << moneyStr << "\n";
This is admittedly verbose, so you may put the first part in a helper function. Also, std::ostringstream formatting might not be the best choice, you can also have a look at std::snprintf (overload 4).
An alternative could be to use std::put_money.
#include <iostream>
#include <locale>
#include <iomanip>
void disp_money(double money) {
std::cout << std::setw(15) << std::showbase << std::put_money(money*100.)<< "\n";
}
int main() {
std::cout.imbue(std::locale("en_US.UTF-8"));
disp_money(12345678.9);
disp_money(12.23);
disp_money(120.23);
}
Output
$12,345,678.90
$12.23
$120.23

std::setw and unicode character

My problem is shown in the following minimal example:
#include <iostream>
#include <string>
#include <iomanip>
int main()
{
int width = 15;
std::cout << std::left;
std::cout << std::setw(width) << "Prints well" << std::setw(width) << "This too" << '\n';
std::cout << std::setw(width) << "\u221E" << std::setw(width) << "This not?" << '\n';
std::cout << std::setw(width+2) << "\u221E" << std::setw(width) << "This is good" << '\n';
}
Compiled using g++, it prints:
Prints well This too
∞ This not?
∞ This is good
So it seems that the unicode symbol uses 3 spaces from the setw instead of one. Is there a simple way to fix this, not knowing beforehand whether a unicode character will be in the string?

Print a decimal number in a particular format using setfill() in c++

I am trying to print a decimal no. in following format : "#####+3.01"
Case: There is a decimal no (let say 3.01 in this case). I have to print it with its sign +/- preceding with y no. of #, with some fix total width. (let say x = 10 in this case).
I tried do something like this :
double no = 3.01;
cout << setfill('#') << setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
But i am getting followinfg output :
+#####3.01
Expected Output :
#####+3.01
Your code gave me correct result. I am using a Linux machine.
Just in case it is a OS dependent problem, try this code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double no = 3.01;
cout << setfill('#') << std::right<< setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
}

C++ manipulation using iomanip library

I am new to C++ STL libraries and need help.
I want to add two numbers suppose A = 4555 and B = 50, and output them as:
4555
+50
4605
Another Examples:
500000 + 12
500000
+12
500012
If i am storing both A and B in integer data type while the sign '+' in character data type. How can i manipulate them to get the preferred output.
I just cant figure out how to manipulate two variables together.
You might utilize the manipulators std::showpos, std::noshowpos and std::setw:
#include <iostream>
#include <iomanip>
int main() {
int a = 4555;
int b = 50;
std::cout
<< std::noshowpos << std::setw(10) << a << '\n'
<< std::showpos << std::setw(10) << b << '\n'
<< std::noshowpos << std::setw(10) << (a+b) << '\n';
}
If you want a width depending on the values you may use three std::ostringstream(s) and create intermediate strings (without setw). After that you print the strings using the maximal length of each for setw:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
int a = 4555;
int b = 50;
std::ostringstream as;
std::ostringstream bs;
std::ostringstream rs;
as << std::noshowpos << a;
bs << std::showpos << b;
rs << std::noshowpos << (a+b);
unsigned width = std::max({ as.str().size(), bs.str().size(), rs.str().size() });
std::cout
<< std::setw(width) << as.str() << '\n'
<< std::setw(width) << bs.str() << '\n'
<< std::setw(width) << rs.str() << '\n';
}
See also:
http://www.cplusplus.com/reference/iomanip/
http://www.cplusplus.com/reference/ios/
Note: You may have a look at the manipulator std::internal.
If you could use constant width (or variable width equal to the maximum width of the numbers involved) with std::setw from <iomanip> as:
#include <iostream>
#include <iomanip>
#include <string>
void display_sum(int a, int b)
{
std::cout << std::setw(10) << a << "\n"
<< std::setw(10) << ("+" + std::to_string(b)) << "\n"
<< std::setw(10) << (a+b) <<"\n" << std::endl;
}
int main()
{
display_sum(4555, 50);
display_sum(500000, 12);
display_sum(503930, 3922);
}
Output:
4555
+50
4605
500000
+12
500012
503930
+3922
507852
Online demo
In your example the fields can fit a maximum number of 7 characters. Perhaps you want to resize the strings to 7 before writing. e.g. fname.resize(7).
To format it as you want you need to #include <iomanip> and use std::left and std::setw(7).
file1 << left << setw(7) << fname
<< tab << setw(7) << lname
<< tab << setw(7) << street
<< tab << setw(7) << city
<< tab << setw(7) << state
<< tab << setw(7) << zip << endl;

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, std::setw, and std::setprecision, but I can't seem to get what I'm looking for. Would love some guidance!
NOTE: The 0-padding isn't really critical, but I'd still like the numbers to be aligned such that the decimal point is in the same column.
It's pretty straightforward
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << setprecision(2) << setfill('0');
cout << setw(5) << 48.3 << endl;
cout << setw(5) << 0.3485 << endl;
cout << setw(5) << 5.2 << endl;
}
Writing code like this makes me yearn for printf however.