Print a string sequentially

Mar 27, 2010 at 2:21pm
Hi everyone,

Anyone knows how to print a string sequentially.
Basically, i made an application (with a GUI) and i want on the screen to print out a string sequentially.

For example: Lets say:
String str = "Hi my name is Mans";

What i would like to do is to print out the string str, character by character with a timer after each character printed out.

See the printing output is the same way as we are writing on the keyboard.

For example, i wanna print out "Hi my name is mans"
So the output is gonna be like:
H (timer = 2seconds) i (timer = 2seconds) m (timer = 2seconds) etc...

Is this clear?


Thanks for u help...

Mar 27, 2010 at 2:31pm
Use a loop to print each character and a sleep function to pause the loop
Mar 27, 2010 at 2:49pm
Thks for replying, but this idea came up to my mind first when i wanted to print the string sequentially.

The sleep() method doesnt do what you are expecting, it freezes the program and then print all the characters at once.

I need something that freeze only the loop when one character has been printed.

Any Idea?
Mar 27, 2010 at 3:04pm
You need to flush the stream after each character
Which sleep function did you use?
Mar 27, 2010 at 3:22pm
Hi, i am actually using wxWidgets and actually its not a string but a wxString, anyway they are similar.

For the sleep function, i just used:
sleep(1);

Here i the code :

1
2
3
4
5
6
7
8
int j = 0;
	for (int i  = 0 ; i < explanation.Length() ; i++)
	{
               // This print the substring(character by character)
		m_dialog->WriteText(explanation.SubString(j,i));
               j++;
		sleep(1);
	}
Mar 27, 2010 at 5:31pm
I never used wxWidgets so I can't help you with this.
Make sure that you don't have to explicitly refresh the dialog to see the result
Mar 29, 2010 at 3:02am
So what command will be used to activate the cursor during the sleep period?.....and who is going to be able to read all these disjointed characters over x lines?
Would you not be better off with a string like:
string s={"A m.....I.....n o t.....j u s t.....s o......s t u p i d "};
cout<<s;
Note: use spaces (" ")I.L.O. dots(.) when formating .
Last edited on Mar 29, 2010 at 3:06am
Topic archived. No new replies allowed.