How to Print 0X0A Instead of 0Xa Using Cout

How can I print 0x0a instead of 0xa using cout?

This works for me in GCC:

#include  <iostream>
#include <iomanip>

using namespace std;

int main()
{
cout << "0x" << setfill('0') << setw(2) << right << hex << 10 << endl;
}

If you are getting sick and tired of iostream's formatting quirkiness, give Boost.Format a try. It allows good-old-fashioned, printf-style format specifiers, yet it is type-safe.

#include <iostream>
#include <boost/format.hpp>

int main()
{
std::cout << boost::format("0x%02x\n") % 10;
}

UPDATE (2019)

Check out the {fmt} library that's been accepted into C++20. Benchmarks show it to be faster than Boost.Format.

#if __has_include(<format>)
#include <format>
using std::format;
#else
#include <fmt/format.h>
using fmt::format;
#endif

std::cout << format("{:#04x}\n", 10);

Printing zero-padded hex with std::cout

I suppose you can write a "stream manipulator". This is useful if you have multiple hex numbers you want to print in this format. This is clearly not an ideal solution, but using a wrapper type you can make your own "format flag" to toggle it. See Sticky custom stream manipulator for more information.

#include <iostream>
#include <iomanip>

static int const index = std::ios_base::xalloc();

std::ostream& hexify(std::ostream& stream) {
stream.iword(index) = 1;
return stream;
}

std::ostream& nohexify(std::ostream& stream) {
stream.iword(index) = 0;
return stream;
}

struct WrapperType {
uint32_t _m;
public:
WrapperType(uint32_t m) : _m(m)
{
}

uint32_t getm() const
{
return _m;
}
};
std::ostream& operator<< (std::ostream& os, const WrapperType& t) {
if (os.iword(index))
return os << "0x" << std::hex << std::setw(8) << std::setfill('0') << t.getm();
else
return os << t.getm();
}

int main()
{
WrapperType my_int{0xabcd};
std::cout << hexify << my_int << my_int;
std::cout << nohexify << my_int;
}

How do I print integer values without additional zeroes after comma by using cout in C++?

std::setprecision specifies the maximum number of digits to use and if you want to get N digits all the time you need to use std::fixed.

#include <iostream>
#include <iomanip>

int main()
{
double a = 3.5;
std::cout << std::fixed;
std::cout << std::setprecision(4);
std::cout << a << "\n";
return 0;
}

And now the output is 3.5000.

Opposite from std::fixed is std::defaultfloat and in the first column you need std::defaultfloat but in the second column you need std::fixed so this is the way to go for you:

cout << defaultfloat << setprecision(2) << arr[i] << " ";
cout << fixed << Strik(arr[i]) << endl;

Check out live

UPDATE

If, as said in one of the comments, you want to output number 13.6, then you need to increase precision like this:

cout << defaultfloat << setprecision(3) << arr[i] << " ";
cout << fixed << setprecision(2) << Strik(arr[i]) << endl;

Can I set a variable that will change the floatfield when using cout?

I/O manipulators are actually functions (!), and the ones you want to switch between happen to have the same signature.

So, you can use a function pointer to achieve your goal:

#include <iomanip>
#include <iostream>

int main()
{
using ManipFuncType = std::ios_base&(std::ios_base&);
const bool condition = true; // or false!

ManipFuncType* floatfield;
float number = 5.894;
if (condition)
floatfield = std::scientific;
else
floatfield = std::fixed;

std::cout << "hello" << floatfield << number << std::endl;
}

(live demo)

How to set output to middle?

Q: Is it possible to write code to make my outputted text formatted to the middle of the screen?

A: Yes. Not with "cout" directly. But certainly with something like ncurses:

  • http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

  • http://invisible-island.net/ncurses/ncurses-intro.html



Related Topics



Leave a reply



Submit