Solved

formatting text output

Posted on 1998-06-11
7
176 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
[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
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
Independent Software Vendors: 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!

 
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

Industry Leaders: 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

761 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