This question already has answers here:
How can I pad an int with leading zeros when using cout << operator? [duplicate]
(7 answers)
Closed 9 years ago.
Here's the code I'm trying to change
string binary = "000000100001000100010000000100000"
bitset<32> set(binary);
cout << hex << set.to_ulong() << endl;
The code shows 2112010 but I want it to show 02112010.
std::cout << std::setfill('0') << std::setw(5) << i << std::endl;
that is the same number you can format it with the 0 by using format specifiers if you need to retain the zero you need to store it as a string,
Related
This question already has answers here:
Cout long double issue
(4 answers)
Closed 6 years ago.
in C++ I'm defining the following types:
double doubleType = 1.6e-300;
long double longDoubleType = 1.6e-300;
I'll then print the values using:
cout << "double is of size " << sizeof(doubleType) << " and value is " << doubleType << endl;
cout << "long double is of size " << sizeof(longDoubleType) << " and value is " << longDoubleType << endl;
my output reads:
double is of size 8 and value is 1.6e-300
long double is of size 12 and value is -1.43863e-264
Whats causing the difference in the interpretation of the values?
It doesn't seem likely that you got this behavior with std::cout. More likely, you were using printf with the wrong format specifier, something like:
printf("%g", longDoubleType);
This is undefined behavior. It's permitted to do just about anything, but one of the common ways it plays out is that some of the bits of longDoubleType are interpreted as a double instead of a long double.
With printf, I can reproduce your output. With std::cout, I can't.
This question already has answers here:
C++: Print out enum value as text
(13 answers)
Closed 8 years ago.
I have a C++ Testing.hh header file in which I have declared this ENUM -
enum TestTypeOpt {TestingOne, TestingTwo};
Now in my Testing.cc class I am trying to print out TestingOne and TestingTwo string from the enum as shown below
cout << "From command" << Testing::TestingOne << endl;
cout << "From command" << Testing::TestingTwo << endl;
But above cout prints out 0 and 1 somehow? I recently started working with C++ so little bit confuse.
Is there anything wrong I am doing? I am just trying to print actual string value from the enum class.
Someone please correct me if there is a better approach, but the way I know to do this is with macros.
Just type #define str(x) #x
This replaces x with the name as it was written in the code.
Then use for example:
cout << "From command" << str(Testing::TestingOne) << endl;
This question already has answers here:
Setting width in C++ output stream
(3 answers)
Closed 9 years ago.
I know how to set field width but only applying to the first element in the stream.
For example.
cout << setw(5) << left << '1' << '2';
produces
1 2
and
cout << setw(5) << left << '1' << '2' << '3';
produces
1 23
How can I use the iomanip library to set the field width so that it applies to all elements
producing
1 2 3
instead of writing setw(5) twice like below:
cout << setw(5) << left << '1' << setw(5) << left << '2' << '3';
Unfortunately, no. You must use setw() before almost every output operation. The problem is that operator<< effectively calls setw(0) after the output, thus you need to set width again. See here for a full list of operations that reset field width.
Note: setw is just a wrapper around width(), so using the latter won't help.
This question already has answers here:
Set the digits after decimal point
(7 answers)
Closed 9 years ago.
C++
I would like to cout float f = 2.3333, but only with two decimals. How do I do that?
I remember something like this, but it doesn't work:
cout << f:2 << endl;
Using stream manipulators fixed and setprecision:
#include <iomanip>
float f = 2.3333;
std::cout << std::setprecision(2) << std::fixed << f;
I managed to solve it without iomanip:
cout << (((int)f*100) % 100)/100;
This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 6 years ago.
I want to output the value of a double in it's full precision. However, when using the cout function, it only displays the first 6 digits even though there is around 15-16 digits of precision.
How do I get my program to display the entire value, including the magnitude (power) component?
Use the setprecision() manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/
You can also force scientific notation with the scientific manipulator:
http://www.cplusplus.com/reference/iostream/manipulators/scientific/
cout << scientific << setprecision(15) << my_number << endl;
you could use something like this :
#include <iomanip>
cout << setprecision (9) << double_value << endl;
more iomanipulators, here
You're looking for setprecision (code taken from link):
int main () {
double f =3.14159;
cout << setprecision(15) << f << endl;
return 0;
}