FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


cout alignment





poiko123
I am relatively new to C++ and my problem involves the output of text. Basically I wish for the text to scroll and not break in the middle of any words. I am not quite sure how to do this. Right now I am just using cout then sleep, cout then sleep, etc. , but this is very tedious and I have a lot of text. Is there an easier way of doing this?
muggle
Please provide more details on the task you’re solving and the environment you’re running it.
Indi
Technically, there is no "sleep" in standard C++, but for the moment, I'll assume there is.

You would have to implement your own breaking, or word wrapping, algorithm. That's not so hard to do.

Let's pretend the message you want to print is:
Quote:
It is three thousand light-years to the Vatican. Once, I believed that space could have no power over faith, just as I believed that the heavens declared the glory of God's handiwork. Now I have seen that handiwork, and my faith is sorely troubled. I stare at the crucifix that hangs on the cabin wall above the Mark VI Computer, and for the first time in my life I wonder if it is no more than an empty symbol.

Let's also pretend that you want lines to be no more than 80 characters long. That means what you actually want to print is:
Code:
It is three thousand light-years to the Vatican.  Once, I believed that space
could have no power over faith, just as I believed that the heavens declared
the glory of God's handiwork.  Now I have seen that handiwork, and my faith
is sorely troubled.  I stare at the crucifix that hangs on the cabin wall
above the Mark VI Computer, and for the first time in my life I wonder if it
is no more than an empty symbol.

With a delay in between each line.

So first you need a function that takes a string and breaks it into a vector of strings of the length you want:
Code:
vector<string> word_wrap(string const& text, unsigned int width);


Then you need a function object to print a string and then delay:
Code:
struct print_line_and_delay : public unary_function<string, void>
{
public:
   explicit print_line_and_delay(unsigned int delay_time) : delay_(delay_time)
   {}
   
   void operator()(string const& line) const
   {
      cout << line << endl; // Flush before delay
      sleep(delay_);
   }
   
private:
   unsigned int delay_;
};

Then the code to actually do it is:
Code:
string text; // Set this to the text above

vector<string> lines = word_wrap(text, 80); // Break it up

// With a 50 ms delay, print them all
for_each(lines.begin(), lines.end(), print_line_and_delay(50));

That's all there is to it. As for the word_wrap() function, I'll leave that for you to write, but here's a hint: find_last_not_of() is your friend.

As with all code I write online, none of this has been tested, or even compiled. Use at your own risk.
poiko123
thank you. i'll have to try this.
Related topics
What software do you use to design?
PS emulater????
Les MMORPG...que choisir?
C++ TUTORIAL : INTRODUCTION TO C++
Image Processing in C++ by Shirish
Stack Implementation by Linked List
convert ur PDF files to word documents
What's wrong with this C code???
C++ Question
CSS layout help
banner
CSS div alignment issue, IE and FF (newbie problem)
Problem with using Dev-C++ Editor
Integer to Binary
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.