Solved

formatting text output

Posted on 1998-06-11
7
175 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:onestar
7 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 50 total points
ID: 1165663
You'll need to write the code yourself, i.e. manipulating the strings.

Start at the position you want to wrap at. Look at the character at that position. If it is a space, separate the original string into two substrings: the substring before that position and the substring after that position.

If it is not a space, then you'll need to look from that position to the beginning of the string, stopping at the first space. Once you have found a space, use the code from above to break up the string into two substrings.

If you do not find a space (i.e. reached the beginning of the string), then truncate at the desired position.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165664
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165665
beat me by minute...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 84

Expert Comment

by:ozo
ID: 1165666
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';
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165667
That's about half the length I would have come up with!
0
 
LVL 1

Author Comment

by:onestar
ID: 1165668
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?
0
 
LVL 1

Author Comment

by:onestar
ID: 1165669
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

789 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