Thread: Console app: Making pretty text blocks

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    Console app: Making pretty text blocks

    Hello everyone!

    I apologize for not being able to find the answer to this question by myself...but as you can see, I am pretty much a C++ noob.

    To practice some C++ I am writing a VERY simple text adventure using beginner-level code and I have quickly found that formatting the text on the screen using cout, \n, and endl is annoyingly cumbersome. The result I want is for lines to be left justified (easily done) and rationally broken at the right of the screen and continued on the next line.

    This seems like such a common task that I reckoned it must be a problem tackled frequently with fairly standard functions, or by some other common coding practice...if anyone could tell me (or point me to a resource) how this is done, that would be great! I also realize that I may need to write a function (is parser the correct term?) to break long strings in this way. I know that seems like a simple task for a lot of you, but I wanted to get some answers before I waste my time reinventing the wheel! Of course, since this is a simple text adventure, part of the complication is that certain words in the strings may be char or numeric variables so it seems sensible to do the formatting on-the-fly.

    Again, your answer might just be to point me to the standard library where the functions or manipulators or whatever exist that can help me on my way. Many MANY thanks for your help in advance!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    169
    There are several ways to break a line when it reaches the end.
    In a default command prompt for example, there are 80 characters in each line. Does that give you any ideas?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4
    Well, given that answers to this topic have been slow coming, I will take your clue as a hint to go ahead and write my own algorithm to terminate lines in a pretty way. It won't be hard to throw in a \n based on whether or not the next word makes the line longer than 80 characters. Thank you for pointing out this number, by the way.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There really is no algorithm for this because a) different platforms uses different amount of character length and b) the length of a console/terminal can be customized and the program will never know and c) perhaps too few cares about these problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4
    Quote Originally Posted by Elysia View Post
    There really is no algorithm for this because a) different platforms uses different amount of character length and b) the length of a console/terminal can be customized and the program will never know and c) perhaps too few cares about these problems.
    Fair enough! That gives me the answer I need. Ty!

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    147
    For c++, boost as a formatting facility that actually works better than printf.

    However, in a non-C++ style, printf provides some of the formatting you required - boost is superior for C++ though.

    It still doesn't deal with line breaks based on text content. That kind of formatting requires what is probably better called rendering than parsing, though parsing input is required in order to know what to render.

    What's just as important to keep in mind, too, is that your solution might be general enough to be reused whenever this comes up in your future work. Developing your own library of code is a good practice.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4
    Quote Originally Posted by JVene View Post
    For c++, boost as a formatting facility that actually works better than printf.

    It still doesn't deal with line breaks based on text content. That kind of formatting requires what is probably better called rendering than parsing

    What's just as important to keep in mind, too, is that your solution might be general enough to be reused whenever this comes up in your future work. Developing your own library of code is a good practice.
    Haha! I actually did come across boost while searching for a solution to my problem...but like you I wasn't convinced it could help me efficiently.

    I am actually in the middle of programming a solution to my issue. It has so far been a good experience. While my solution is not yet complete, I am having to learn how to deal well with strings, to recognize ASCII characters, etc. And as you say, it will be a bit of code that I might get some mileage out of. Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. newbie question about storing large blocks of text.
    By DimensionX in forum C Programming
    Replies: 3
    Last Post: 11-27-2005, 05:54 PM
  3. Console App minimized into system tray
    By Liam Battle in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2002, 01:29 AM
  4. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM