Solved

formatting text output

Posted on 1998-06-11
7
178 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

687 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