Link to home
Start Free TrialLog in
Avatar of onestar
onestar

asked on

formatting text output

I want to output a string but i want to limit width of the column how do i do this?

string = "hello this is a test for width"

I want it to look like this:

cout  << string;

     hello this
     is a test
     for width

I know cout.width (5) will start 5 over but what will stop it at 15 plus i want it to wrap the word to the next line if the whole word won't fit on that line.
ASKER CERTIFIED SOLUTION
Avatar of Tommy Hui
Tommy Hui

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

There is no built in feature for doing so, you will have to do it "manually" by counting the characters you output and wrapping as needed.

Do you need help implimenting that?
beat me by minute...
Avatar of ozo
char *string = "hello this is a test for width";
char *s0 = string;
char *s1;
while( strlen(s0) > 10 && (s1 = strchr(s0,' ')) ){
        char *s2;
        while( (s2 = strchr(s1+1,' ')) && s2-s0 <= 10 ){ s1=s2; }
        *(s0 = s1) = '\n';
}

That's about half the length I would have come up with!
Avatar of onestar

ASKER

That works but I need it to start at certain amount of spaces over aswell.  I tried a few things but I could only get the first part over.

Ex.
     hello this
is a test
for width

Any suggestions?
Avatar of onestar

ASKER

How would I do this if I want to ask the user for the string?  When I tried this it would only put one word per line until the last line which it had two.