The `CryptoPP::Integer` alignment during output - c++

I'm trying to output values of the CryptoPP::Integer type nicely using manipulators. My small test is below:
#include <iomanip>
#include <iostream>
#include <crypto++/integer.h>
int main()
{
std::cout << "int, aligned to the right : " << std::setw(6) << std::right << int(100) << std::endl;
std::cout << "CryptoPP::Integer, aligned to the right: " << std::setw(6) << std::right << CryptoPP::Integer(100) << std::endl;
std::cout << "int, aligned to the left : " << std::setw(6) << std::left << int(100) << std::endl;
std::cout << "CryptoPP::Integer, aligned to the left : " << std::setw(6) << std::left << CryptoPP::Integer(100) << std::endl;
}
The result looks like this:
int, aligned to the right : 100
CryptoPP::Integer, aligned to the right: 100.
int, aligned to the left : 100
CryptoPP::Integer, aligned to the left : 1 00.
So, the alignment of the CryptoPP::Integer value to the right doesn't work correctly, and the alignment to the left breaks the value into two parts. Also a . in the end?
Is it a known bug? How to deal with it?
(crypto++ version - 5.6.4-8)

Related

Using stringstream's ignore

I am trying to use a stringstream as a buffer but I am unable to update the underlying streambuf :
#include <iostream>
#include <sstream>
int main () {
std::stringstream ss(std::ios_base::app|std::ios_base::in|std::ios_base::out); //ostringstream gives the same output
ss << "foo";
std::cout << "position get: " << ss.tellg() << std::endl;
std::cout << "position put: " << ss.tellp() << std::endl;
ss << "b";
std::cout << "position get: " << ss.tellg() << std::endl;
std::cout << "position put: " << ss.tellp() << std::endl;
char c;
ss >> c;
std::cout << "position get: " << ss.tellg() << std::endl;
std::cout << "position put: " << ss.tellp() << std::endl;
ss.ignore(1);
std::cout << "position get: " << ss.tellg() << std::endl;
std::cout << "position put: " << ss.tellp() << std::endl;
std::cout << ss.str() << std::endl;
return 0;
}
yields:
position get: 0
position put: 3
position get: 0
position put: 4
position get: 1
position put: 4
position get: 2
position put: 4
foob
Is it possible to force a reallocation of the streambuf underlying object ? If not, is this reallocation automatic and in which circumstance is it triggered ?
I know I can use ss.str to change the underlying buffer. But it is a pain to use it to manually update the buffer to a substr's version.
Note: I am doing a school project and must compile to c++98, hence, if you have a solution which is comptabible, it would be much appreciated.

How to format right and left justification on the same line?

If I am making a data table to show the results of several functions, how can I use the setw(), left, and right keywords to create a table which is formatted like this:
Height 8
Width 2
Total Area 16
Total Perimeter 20
Notice how the overall "width" of the table is constant (about 20 spaces). But the elements on the left are left justified and the values on the right are right justified.
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
struct Result
{
std::string Name;
int Value;
};
int main()
{
std::vector<Result> results = { {"Height", 8}, {"Width", 2}, {"Total Area", 16}, {"Total Perimeter", 20} };
for (auto result : results)
{
std::cout << std::setw(16) << std::left << result.Name;
std::cout << std::setw(4) << std::right << result.Value << std::endl;
}
return 0;
}
You could do something like this:
// "Total Perimiter" is the longest string
// and has length 15, we use that with setw
cout << setw(15) << left << "Height" << setw(20) << right << "8" << '\n';
cout << setw(15) << left << "Width" << setw(20) << right << "2" << '\n';
cout << setw(15) << left << "Total Area" << setw(20) << right << "16" << '\n';
cout << setw(15) << left << "Total Perimeter" << setw(20) << right << "20" << '\n';

What's the Difference Between floor and duration_cast?

So in c++11 the Chrono Library provides, duration_cast:
Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished
And c++17's floor:
Returns the greatest duration t representable in ToDuration that is less or equal to d
So for all x will the result of these 2 calls be equal:
chrono::duration_cast<chrono::seconds>(x)
chrono::floor<chrono::seconds>(x)
As far as I can tell, same as the difference between static_cast and std::floor: Negatives are rounded down instead of truncated toward zero.
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
int main() {
std::cout << "duration_cast:" << std::endl;
std::cout << "1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(1400ms).count() << std::endl;
std::cout << "1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(1500ms).count() << std::endl;
std::cout << "1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(1600ms).count() << std::endl;
std::cout << "-1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(-1400ms).count() << std::endl;
std::cout << "-1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(-1500ms).count() << std::endl;
std::cout << "-1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(-1600ms).count() << std::endl;
std::cout << "floor:" << std::endl;
std::cout << "1.4s: " << std::chrono::floor<std::chrono::seconds>(1400ms).count() << std::endl;
std::cout << "1.5s: " << std::chrono::floor<std::chrono::seconds>(1500ms).count() << std::endl;
std::cout << "1.6s: " << std::chrono::floor<std::chrono::seconds>(1600ms).count() << std::endl;
std::cout << "-1.4s: " << std::chrono::floor<std::chrono::seconds>(-1400ms).count() << std::endl;
std::cout << "-1.5s: " << std::chrono::floor<std::chrono::seconds>(-1500ms).count() << std::endl;
std::cout << "-1.6s: " << std::chrono::floor<std::chrono::seconds>(-1600ms).count() << std::endl;
return 0;
}
.
duration_cast:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -1
-1.5s: -1
-1.6s: -1
floor:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -2
-1.5s: -2
-1.6s: -2
https://wandbox.org/permlink/SsmpRz6RkvbL6Sru

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;

std::setw() considering special characters as two characters

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("®èé")