prepending number with 0's [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Print leading zeros with C++ output operator (printf equivalent)?
#include <iostream>
#include <iomanip>
int main()
{
int n = 16;
std::cout << ???(5) << n << std::endl;
}
I want the output to be 00016
setw() prepends with spaces. Isn't it configurable what characters to prepend with setw()?
My eventual goal is to print a hex 2-byte number in 4 positions. Something like this:
#include <iostream>
#include <iomanip>
int main()
{
unsigned short n = 0xA7;
std::cout << std::hex << ???(4) << n << std::endl;
}
and I am expecting to get this output: 00A7

You also need setfill('0').

Related

String to ascii hex/int value [duplicate]

This question already has answers here:
uint8_t can't be printed with cout
(8 answers)
Closed 3 years ago.
I am trying to convert a string to a vector of ASCII value uint8_t
For example, if I have a string that is "500" I would want a vector that is {53, 48, 48} where these values are hex 0x35, 0x30, 0x30. This is what I am currently doing and it is not working
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string number = "500";
std::vector<std::uint8_t> bytes;
for (auto& c : number)
bytes.push_back(static_cast<std::uint8_t>(c));
for (auto& c : bytes)
std::cout << c << std::endl;
return 0;
}
But I just get 5, 0, 0 as output where I was expecting 53, 48, 48
std::cout << c << std::endl; is writing std::uint8_t as a character (see this question for details).
You have to cast it to an integer to actually get the result you want.
For example (wandbox link):
std::cout << static_cast<int>(c) << std::endl;

Why vector.size()-1 gives garbage value? [duplicate]

This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code
myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.

lexical_cast strtof strtold lose accuracy? [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 6 years ago.
Here:
#include <iostream>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
int main(void) {
const char * str = "277499.84";
std::cout << boost::lexical_cast<double>(str) << std::endl;
std::cout << strtof(str, NULL) << std::endl;
std::cout << strtold(str, NULL) << std::endl;
std::cout << atof(str) << std::endl;
return 0;
}
output:
277500
277500
277500
277500
Why the output are not 277499.84?
It's not the operations themselves losing accuracy, but the output.
You can use the I/O manipulator std::setprecision to control the numeric precision. The following will use the full precision of a double (assuming the stream is set for decimal output).
double value = boost::lexical_cast<double>(str);
std::cout << std::setprecision( std::numeric_limits<double>::digits10 + 1 ) << value;
Or you can use std::ios_base::precision. This is useful if you want to restore the precision to the original value after.
auto old_precision = cout.precision( std::numeric_limits<double>::digits10 + 1 );
cout << value;
cout.precision( old_precision );

Number of digits for an int to use in a file name in c++ [duplicate]

This question already has answers here:
Print leading zeros with C++ output operator?
(6 answers)
Closed 8 years ago.
I need to use some integer values as a part of some file names I create as outputs in a c++ program. For this I use the code above. The thing is my int values ( named n_irrad in the code) goes from 1 to 20000 for example, so I need the file names to be MC_Interface00001.txt, MC_Interface00002.txt, ..., MC_Interface20000.txt. So, ¿how can I set the number of digits in the file name? With the code I'm using I obviously get MC_Interface1.txt, MC_Interface2.txt, ..., MC_Interface20000.txt.
Thanks in advance.
ofstream MC_Interface;
string Irrad = static_cast<ostringstream*>( &(ostringstream() << n_irrad) )->str();
string MC_Interface_FileName = "MC_Interface" + Irrad + ".txt";
MC_Interface.open(MC_Interface_FileName.c_str());
Try the following
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
std::ostringstream os;
os << std::setw( 5 ) << std::setfill( '0' ) << 10;
std::string s = "MC_Interface" + os.str();
std::cout << s << std::endl;
return 0;
}
The output is
MC_Interface00010
ofstream MC_Interface;
std::stringstream ss("MC_Interface");
ss << std::setw(5) << std::setfill('0') << n_irrad;
ss << ".txt";
MC_Interface.open(ss.str());

max possible int in c++ [duplicate]

This question already has answers here:
maximum value of int
(7 answers)
Closed 9 years ago.
I thought that unsigned int max possible value is 65535 ( in C++ ) but I created a programm which can use an int which is equal with 100000 for example. Is it safe to use int with values up to 10000000 or the program may crash? In that case is long the only solution?
Thank you for your time!
Use std::numeric_limits<unsigned int>::max() to know for certain what this value is.
Please check the code below for limits:
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
int main () {
std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
return 0;
}
INT_MAX is implementation defined. That means it's up to your compiler vendor to decide, as long as it's no less than 32767, and greater or equal to a short. You can use the climits definitions to discover your implementation's limits:
#include <iostream>
#include <climits>
int main () {
std::cout << INT_MAX << std::endl;
return 0;
}
On my installation of gcc/g++ v4.8.1 targeting x86_64-linux-gnu, this snippet produces:
2147483647
And as has been mentioned in the followup replies to this answer, you may (and probably should) use the more semantically proper (for C++) method:
#include <iostream>
#include <limits>
int main () {
std::cout << std::numeric_limits<int>::max() << std::endl;
return 0;
}
...which ought to produce the same output.