C++ manipulation using iomanip library - c++

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;

Related

Cpp/C++ Output allingment in one line from right AND left

I need to write ints from the right and strings from the left into a single line and have them line up properly (view output below the code).
Basically I just need a way to write a table only using iostream and iomanip and change the allingment from right for ints to left for strings and back.
Other tips are also appreciated :)
#include <iostream>
#include <iomanip>
using namespace std;
class foo
{
public:
int i;
std::string s;
int j;
foo(int i1,std::string s1,int j1) : i(i1), s(s1),j(j1) {};
};
int main()
{
foo f1(1, "abc",50);
foo f2(100, "abcde",60);
cout << resetiosflags(ios::adjustfield);
cout << setiosflags(ios::right);
cout << setw(6) << "i" << setw(15) << "s" << setw(15) << "j"<<endl;
cout << setw(8) << f1.i << setw(15)
<< resetiosflags(ios::adjustfield) << setiosflags(ios::left) << f1.s <<setw(5)
<< resetiosflags(ios::adjustfield) << setiosflags(ios::right) << setw(15) << f1.j << endl;
cout << setw(8) << f2.i << setw(15)
<< resetiosflags(ios::adjustfield) << setiosflags(ios::left) << f2.s <<setw(5
<< resetiosflags(ios::adjustfield) << setiosflags(ios::right) << setw(15) << f2.j << endl;
/*i s j
1abc 50
100abcde 60*/
return 0;
}
This is the output:
i s j
1abc 50
100abcde 60
And this is what i need:
i s j
1 abc 50
100 abcde 60
Using left and right in the same line isn't a problem. It looks like the issue you have is not allowing for space after the first value. It looks like the setw(5) may have been for that, but since there's nothing printed after it there's no effect. I used 7 to match the 15 total used for the string width.
Maybe something like this would work? Probably best to extract the magic numbers into constants so you can adjust all of them easily in one place. You could also wrap this in an operator<< to contain all the formatting code in one place.
The first line headings offset one to the left looks weird to me, but it matches your example and is easy to adjust if necessary.
#include <iostream>
#include <iomanip>
using namespace std;
class foo
{
public:
int i;
std::string s;
int j;
foo(int i1, std::string s1, int j1) : i(i1), s(s1), j(j1)
{};
};
int main()
{
foo f1(1, "abc", 50);
foo f2(100, "abcde", 60);
cout
<< right << setw(7) << "i" << setw(7) << ' '
<< left << setw(15) << "s"
<< right << "j"
<< endl;
cout
<< right << setw(8) << f1.i << setw(7) << ' '
<< left << setw(15) << f1.s
<< right << f1.j
<< endl;
cout
<< right << setw(8) << f2.i << setw(7) << ' '
<< left << setw(15) << f2.s
<< right << f2.j
<< endl;
return 0;
}
Output:
i s j
1 abc 50
100 abcde 60

I want to align my results in columns based on the decimal place

I want to display my result aligned on the column by the decimal place.
I have tried to just put setw(7) and setw(6) in parts of the display but it seems to not change the output at all.
int main()
{
double x;
char more = 'y';
while(more=='y' || more=='Y')
{
cout << "\n\t\t\tInput x:";
cin >> x;
cout << "\n\n\t\t\t LibraryResult\tMyResult" << endl;
cout << setprecision(2) << fixed << "\n\t\tsin(" << x << ")\t"
<< setprecision(6) << sin(x) << "\t" << mySin(x) << endl;
cout << setprecision(2) << fixed << "\n\t\tcos(" << x << ")\t"
<< setprecision(6) << cos(x) << "\t" << myCos(x) << endl;
cout << setprecision(2) << fixed << "\n\t\texp(" << x << ")\t"
<< setprecision(6) << exp(x) << "\t" << myExp(x) << endl;
}
}
I want the resultants of the program to be aligned by decimal, so when you put in a number like 2 the decimals are all in the same column.
If you set a precision of 6, a width of 6 or 7 is simply not enough to contain the number. You have to either reduce the precision or increase the width.
Try playing around with the stream manipulators in the following snippet
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
int main()
{
const double period = 2 * 3.141592653589793;
const int steps = 20;
std::cout << std::string(44, '=') << '\n';
std::cout << std::setw(3) << "x" << std::setw(12) << "sin(x)"
<< std::setw(12) << "cos(x)" << std::setw(14) << "tan(x)" << '\n';
std::cout << std::string(44, '-') << '\n';
for(int i = 0; i <= steps; ++i)
{
double x = i * period / steps;
std::cout << std::setprecision(2) << std::fixed << x
<< std::setprecision(6) << std::setw(12) << std::sin(x)
<< std::setprecision(6) << std::setw(12) << std::cos(x)
<< std::setprecision(6)
<< std::setw(16) // <- Try to decrease it
<< std::scientific // <- Try to keep it as std::fixed
<< std::tan(x) << '\n';
}
std::cout << std::string(44, '-') << '\n';
}
You must use several manipulators (one is not enough)
You might want to try with the following combination, that right-aligns your numbers, with a fixed number of decimals.
std::cout.width(15);
std::cout << std::fixed << std::setprecision(6) << std::right << exp(x) << std::endl;
You can find information about manipulators here

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

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.

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?

In C ,we use %x.ys for string manipulation. What it will be in C++?

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