Thread: Silly question about sentence formatting

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Silly question about sentence formatting

    Hi,
    I'm a college kid just starting to learn C++, so please bear with me, though I do grasp most basic concepts of the language.

    Anyway, I have an unusual question: When outputting text to the console (via cout), is there either some sort of code or some sort of convention for making the text wrap in an aesthetically appealing way?

    For example, suppose I want to print the text "Hello. I am completely ridiculous. I inhaliere the paste, because I know not somehow to improve. That is, because I am idiot one gugein'." Clearly this sentence will not fit on one line in the console, but the way it's cut off makes it terrible, like this:

    Hello. I am completely ridiculous. I inhaliere the paste, because I know not som
    ehow to improve. That is, because I am idiot one gugein'.

    Obviously having a word cut in half like that is unacceptable, so is there something I can do that will yield a pleasant word wrap effect for output? Is the convention simply to place a \n or endl wherever needed, via trial and error?

    Any insight is appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so is there something I can do that will yield a pleasant word wrap effect for output?
    Last I checked, you had to do it manually. That is, find the width of the screen, find the first word break before the width, and insert a newline so that words aren't split.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    Thanks. That's what I was afraid of. But anyway, if there's no other way to do it, I won't feel so clueless doing it this way myself.

    Much appreciated.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Of course, if you see yourself doing this a lot, you can create a whole new output class and overload <<
    Gotta love C++ for being able to do that
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you can create a whole new output class and overload <<
    Two words: custom manipulator.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Silly "extern" question
    By mwkaufman in forum C Programming
    Replies: 2
    Last Post: 06-10-2006, 01:50 PM
  2. Sreen Resolution Statistics?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 04-26-2004, 02:33 PM
  3. Question about formatting flags
    By dpro in forum C Programming
    Replies: 2
    Last Post: 12-24-2002, 04:46 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. formatting question..
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 01:01 PM