0

How can I print an string with padding in C++? Specifically what I want is:

cout << "something in one line (no prefix padding)"
cout << "something in another line (no prefix padding)"
set_padding(4)
    cout << "something with padding"
    cout << "something with padding"
set_padding(8)
        cout << "something with padding"
        cout << "something with padding"

That is, I'll call cout many times and I don't want to call setw(len) << "" all the time.

7

3 Answers 3

4

I suppose you could just make the pre-processor type it for you:

#include <iostream>
#define mout std::cout << std::string(width,' ')
#define mndl "\n" << std::string(width,' ')

int width=0;

int main()
{
    mout << "Hello" << std::endl; 

    width = 8;

    mout << "World." << mndl << "Next line--still indented";
    // more indented lines...
}
4
  • Although it wasn't what I was expecting, it is a nice approach. thanks. Aug 11, 2012 at 19:19
  • The unique problem is that I can't use endl inside a statement like this: mout << "line1" << endl << "line2"; Anyway, good answer. Aug 11, 2012 at 19:35
  • Unless you do #define endl "\n" << std::string(width,' ') :) Aug 11, 2012 at 19:44
  • rsrsrrs the interesting thing of your sugestion is that I can continue using cout whenever I want. I "redefine" endl all goes for nothing. But I could define a "pendl" =D Aug 11, 2012 at 19:49
3

How about something like:

class IndentedOutput
{
public:
    IndentedOutput()
    {
        m_indent = 0;
    }

    void setIndent(unsigned int indent)
    {
        m_indent = indent;
    }

    template <typename T>
    std::ostream& operator<< (const T& val)
    {
        return (std::cout << std::string(m_indent,' ') << val);
    }

private:
    unsigned int m_indent;
};

And the you can use it like this:

IndentedOutput ind;

int i =0;
ind << "number is " << i++ << '\n';
ind << "number is " << i++ << '\n';
ind.setIndent(4);
ind << "number is " << i++ << '\n';
ind << "number is " << i++ << '\n';
ind.setIndent(6);
ind << "number is " << i++ << '\n';
ind << "number is " << i++ << '\n';
ind.setIndent(0);
ind << "number is " << i++ << '\n';
-1

http://www.cplusplus.com/reference/iostream/manipulators/

EDIT: Sorry, I was a little short handed. What I meant was to research how iomanip works, I know you are aware of sets(n) << ""

Using iomanip as a baseline for a quick and dirty implementation, this is what I came up with:

#include <iostream>
#include <iomanip>
#include <string>

class setpw_ {
  int n_;
  std::string s_;
public:
  explicit setpw_(int n) : n_(n), s_("") {}
  setpw_(int n, const std::string& s) : n_(n), s_(s) {}

  template<typename classT, typename traitsT>
  friend std::basic_ostream<classT, traitsT>&
  operator<<(std::basic_ostream<classT, traitsT>& os_, const setpw_& x) {
    os_.width(x.n_);
    os_ << "";
    if ( x.s_.length()){
      os_ << x.s_;
    }
    return os_;
  }
};

setpw_ setpw(int n) { return setpw_(n); }
setpw_ indent(int n, const std::string& s) { return setpw_(n, s); }

int
main(int argc, char** argv) {
  std::cout << setpw(8) << "Hello, World" << std::endl;
  std::cout << indent(8,   "----^BYE^---") << std::endl;
  return 0;
}
1
  • 2
    If you think I can accomplish what I want using manipulators, please complete your answer appropriately. Aug 11, 2012 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.