Output formatting

This is a discussion on Output formatting within the C++ Programming forums, part of the General Programming Boards category; Hello, Is it possible to tell cout that from now on, all output must be indented with n spaces? For ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    Output formatting

    Hello,

    Is it possible to tell cout that from now on, all output must be indented with n spaces?

    For example:
    cout << "blahblah" << indent(3) << endl;
    cout << "abc" << endl;
    cout << "123" << endl;

    would output:
    Code:
    blahblah
       abc
       123

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not that I'm aware of.
    You could have an object that generates a suitably indented string, e.g:
    Code:
    class Indent
    {
    private:
       unsigned int level;
    public:
       Indent(int n = 0): level(n) {}
       ~Indent() {};
       void setLevel(int n) { level = n; };
       operator std:string()
       {
          return std::string(level, ' ');
       }
    };
    
    int main()
    {
       Indent ind;
    
       std::cout << ind << "Hello, world" << std::endl;
       ind.setLevel(3);
       std::cout << ind << "abc" << std::endl;
       std::cout << ind << "123" << std::endl;
       return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thanks mats, but I need a method where the output producing code is unaware of the indents. I guess that's not possible.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How about replacing endl with a string? This could contain only '\n' or '\n' followed by a number of spaces.

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
         std::string newline("\n");
         std::string newline_plus_spaces = newline + std::string(3, ' ');
         
         std::string end_line = newline_plus_spaces;
         std::cout << "Hello, world" << end_line;  //next line is indented
         std::cout << "abc" << end_line;  //same
       
         end_line = newline;
         std::cout << "123" << end_line;  //next line is not indented
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21