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?
Related
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.
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
How can I replace printf() with cout?
My Code In C++:
#include<iostream>
#include<cstdio>
using namespace std;
int main ()
{
char st[15]="United Kingdom";
printf("%5s\n",st);
printf("%15.6s\n",st);
printf("%-15.6s\n",st);
printf("%.3s\n",st); // prints "Uni"
return 0;
}
The Code Prints:
United Kingdom
United
United
Uni
How can I manipulate like this in C++?
The std::setw() I/O manipulator is the direct equivalent of printf()'s minimum width for strings, and the std::left and std::right I/O manipulators are the direct equivalent for justification within the output width. But there is no direct equivilent of printf()'s precision (max length) for strings, you have to truncate the string data manually.
Try this:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
char st[15] = "United Kingdom";
cout << setw(15) << st << '\n'; // prints " United Kingdom"
cout << setw(5) << st << '\n'; // prints "United Kingdom"
cout << setw(15) << string(st, 6) << '\n'; // prints " United"
cout << left << setw(15) << string(st, 6) << '\n'; // prints "United "
cout << setw(15) << string(st, 0) << '\n'; // prints " "
cout << string(st, 3) << '\n'; // prints "Uni"
cout << st << '\n; // prints "United Kingdom"
return 0;
}
Live demo
The std::setw() iomanip will set the field width for insertions, that is, even if the isinsertions shorter than the specified lenth it will get padded to that amount, std::setfill() sets the padding character.
For example:
std::cout << std::setw(20) << std::setfil('0') << "hey you!" << std::endl;
would print "Hey you!000000000000\n" (possibly \r\n instead of just \n if you're using Windows). There is also std::left which would cause the padding to precede rather than follow the insert.
You are looking for Boost.Format.
#include <iostream>
#include <string>
#include <boost/format.hpp>
int main()
{
std::string st = "United Kingdom";
std::cout << boost::format("%15s\n") % st;
std::cout << boost::format("%5s\n") % st;
std::cout << boost::format("%15.6s\n") % st;
std::cout << boost::format("%-15.6s\n") % st;
std::cout << boost::format("%15.0s\n") % st;
std::cout << boost::format("%.3s\n") % st; // prints "Uni"
std::cout << boost::format("%s\n") % st;
}
Live example
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;
Why is std::setw() considering special chars as two chars ? Is there any easy and stylish way to solve this ?
Eg :
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::left << std::setw(10) << "ok" << "ok" << std::endl;
std::cout << std::left << std::setw(10) << "test.." << "ok again" << std::endl;
std::cout << std::left << std::setw(10) << "®èé" << "fail" << std::endl;
return 0;
}
Ouputs :
ok ok
test.. ok again
®èé fail
Here is the live test : http://ideone.com/q57I0H
They are two characters, check the value of sizeof("®èé")