Solved

indent std::cout ?

Posted on 1998-12-06
4
2,951 Views
Last Modified: 2013-12-14
is there a way to produce indented text with std::cout? I want to be able to do something like this:

std::cout << "text (normal indentation).\n";
std::cout << indent(4) << "text indented by 4 spaces\nwhich also wraps lines.\n";
std::cout << "normal text again.\n";

to yield:

text (normal indentation).
    text indented by 4 spaces
    which also wraps lines.
normal text again.

I know this is easy if I specifically state '\n', but can I indent text which virtually wraps after, say 80 characters (but not in the middle of a word)?
is this possible in C++ with STL libraries? if not, how would I implement this. If someone helps me with the implementaion, I will raise points!!!
0
Comment
Question by:MaDdUCK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Expert Comment

by:nbell
ID: 1179398
Since you are using string literals perhaps the following code would work for you:

#include <iostream.h>
#include <iomanip.h>

const char *indent(int x) {
      char *p=new char[x];
      for (int i=0;i<x;i++){
            p[i]=*(" ");
      }
      p[x]=NULL;
      return p;
}

int main(int argc, char* argv[])
{
cout << setw(80)<<setiosflags(ios::left)<<"text (normal indentation)."
     << indent(4)<< setw(76)<<setiosflags(ios::left)<<"text indented by 4 spaces"
     << indent(4)<< setw(76)<<setiosflags(ios::left)<<"which also wraps lines.";
cout << "normal text again.\n";
      return 0;
}



I am not sure that indent(int) is already available anywhere.  That code is rather ugly though.

Perhaps a more elegant solution is to use your own string package in which the stream insertion operator << is overloaded to do the "wrapping" you desire.
0
 

Accepted Solution

by:
DOMINIC011899 earned 50 total points
ID: 1179399
#include <IOSTREAM>
#include <IOMANIP>
#include <STRING>
#include <STRSTREAM>
using namespace std;


template <class TP0, class TP1, class TP2>
class OMANIP_INDENTWRAP
    {
    TP0 m_var0;
    TP1 m_var1;
    TP2 m_var2;
    typedef ostream &(*PFN)( ostream &, TP0, TP1, TP2 );
    PFN m_pfn;

    public:
    OMANIP_INDENTWRAP( PFN pfunc, TP0 data0, TP1 data1, TP2 data2  )
        : m_pfn( pfunc ), m_var0( data0 ), m_var1( data1 ), m_var2( data2 )
        {}

    friend ostream &operator<<(
        ostream &os, OMANIP_INDENTWRAP<TP0, TP1, TP2> &robj )
        {
        return (*robj.m_pfn)( os, robj.m_var0, robj.m_var1, robj.m_var2 );
        }
    };

ostream &FuncIndentWrap( ostream &os, const char *szText,
    int nIndent, int nWidthMax )
    {
    istrstream isText( szText );
    char szLineBuf[80] = "";
    int nWidthAccum = 0;

    for ( int n=0;  n<nIndent;  n++ )
        os << ' ';                  

    while ( isText.getline( szLineBuf, sizeof( szLineBuf ), ' ' ) )
        {
        nWidthAccum += strlen( szLineBuf ) + 1;

        if    ( nWidthAccum > nWidthMax )
            {
            os << endl;
            for ( int n=0;  n<nIndent;  n++ )
                os << ' ';
            nWidthAccum = strlen( szLineBuf ) + 1;
            }

        os << szLineBuf << ' ';
        }

    return os;
    }


OMANIP_INDENTWRAP<const char *, int, int> IndentWrap(
    const char *szText, int nIndent, int nWidthMax )
    {
    return OMANIP_INDENTWRAP<const char *, int, int>(
        FuncIndentWrap, szText, nIndent, nWidthMax );
    }


void main()
    {
    char szText[80] = "This will indent the text 4 spaces and wrap after 15 chars.";
    cout << IndentWrap( szText, 4, 15 ) << endl;
    cin.get();
    }

// The actual result:
    This will
    indent the
    text 4 spaces
    and wrap after
    15 chars.

0
 

Expert Comment

by:DOMINIC011899
ID: 1179400
I posted my answer (which is a C++ code) but a different thing came up in the answer box.  Whatever is in that answer box is not from me.  Below is my answer:

#include <IOSTREAM>
#include <IOMANIP>
#include <STRING>
#include <STRSTREAM>
using namespace std;


template <class TP0, class TP1, class TP2>
class OMANIP_INDENTWRAP
    {
    TP0 m_var0;
    TP1 m_var1;
    TP2 m_var2;
    typedef ostream &(*PFN)( ostream &, TP0, TP1, TP2 );
    PFN m_pfn;

    public:
    OMANIP_INDENTWRAP( PFN pfunc, TP0 data0, TP1 data1, TP2 data2  )
        : m_pfn( pfunc ), m_var0( data0 ), m_var1( data1 ), m_var2( data2 )
        {}

    friend ostream &operator<<(
        ostream &os, OMANIP_INDENTWRAP<TP0, TP1, TP2> &robj )
        {
        return (*robj.m_pfn)( os, robj.m_var0, robj.m_var1, robj.m_var2 );
        }
    };

ostream &FuncIndentWrap( ostream &os, const char *szText,
    int nIndent, int nWidthMax )
    {
    istrstream isText( szText );
    char szLineBuf[80] = "";
    int nWidthAccum = 0;

    for ( int n=0;  n<nIndent;  n++ )
        os << ' ';                  

    while ( isText.getline( szLineBuf, sizeof( szLineBuf ), ' ' ) )
        {
        nWidthAccum += strlen( szLineBuf ) + 1;

        if    ( nWidthAccum > nWidthMax )
            {
            os << endl;
            for ( int n=0;  n<nIndent;  n++ )
                os << ' ';
            nWidthAccum = strlen( szLineBuf ) + 1;
            }

        os << szLineBuf << ' ';
        }

    return os;
    }


OMANIP_INDENTWRAP<const char *, int, int> IndentWrap(
    const char *szText, int nIndent, int nWidthMax )
    {
    return OMANIP_INDENTWRAP<const char *, int, int>(
        FuncIndentWrap, szText, nIndent, nWidthMax );
    }


void main()
    {
    char szText[80] = "This will indent the text 4 spaces and wrap after 15 chars.";
    cout << IndentWrap( szText, 4, 15 ) << endl;
    cin.get();
    }


// This is the actual result
    This will
    indent the
    text 4 spaces
    and wrap after
    15 chars.

0
 

Expert Comment

by:DOMINIC011899
ID: 1179401
what's happening here, my comments and answers are not getting through!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is the Best Editor for PHP Development ? 5 110
trigs fail! I thought I knew how to do trignometry 3 94
max float value 3 73
learn programming 8 94
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question