Hi Meesa,
Just wondering, did you try any of my code - or an easily adapted version of it?
Also, am a bit confused as to the exact format of your input / output. Could you show some actual examples of input along with the expected output, and confirm whether the input needs to be modified or not? Is the position of the last space guaranteed to be less than the Field Width? Will there be spaces before position 47?
I still have the impression you are writing too much code for what you need to do, although it is good that you are using the STL functions now. I can see that you a building a new string, which is fine, but I have a strong impression that there is no need to do that, just
cout
substr
's
Have cherry picked these bits out of previous posts:
Other Output A string
of text. <- this was to be right aligned but couldn't format it like that here
Is this printing with a Field Width, & right aligning the second line? Could use the last example.
With my last example, it uses the
setfill
function to pad the output, I specified a space char to do it with. The default for this is to right align the output, but you can change by using
std::left
or
std::right
.
1 2 3
|
/* Other code*/
<< " " << multiLineString(books[i].bookTitle, 55, 21) << endl
<< " " << books[i].bookTitle << endl;
|
How do you know what the padding is in advance?
It occurred to me that even though this accepts a char*, it's for output. |
So, can you could use the last example with a
const
string?
Another approach when using
const
strings, is to use
find
&
substr
to build a new string, but it sounds as though this might not be necessary.
Well, basically I need to read the first 50 characters, unless the spaces are at positions 47 and 53, then I'd need to just read the first 47. |
Could you adapt my second example to work with this, using the find algorithm, and printing substrings? Or, if there is no need to modify the string, just cout the the substrings? You could do this with any of the first 3 examples.
.... as on my TODO list is to add an option so it will break it at words instead of characters. ....
|
This is the second example again.
Another idea, is to find out where the spaces are using the second example, but
push_back
the found positions into a local
std::vector
. Make use of these values to decide what to do next. Use the values as arguments for
substr
's
I hope you don't mind all these suggestions, I am just trying to strive for an elegant solution for you.