How to print slowly

Hi Guys

A few days ago i saw a program that printing(cout) words slowly

In normal cout - it will print the word immediately
I want to know how to print the word by character,character.

For example
Instead of printing" Hello" immediately
I want to know how to print it like this :-
print "H" then after a fraction of a seconds print "e" and so..

I heard you can write it as" nested for loop ".

Can someone teach me how to do that please?
Thanks
Output one character at a time.
pause between character outputs.
Pause might ask for the user to "press any key to continue." If you have access to it, use Sleep(). Or you can use a for loop to run x times the length of the character array you want to print, and use a modulus to print every ith iteration of the loop.
Thanks for help

I wrote it like this - very easy :)

1
2
3
4
5
6
7
8
9
string s="hello";

for(int j=0; j< s. size( ) ; j ++)
{

     for(int i=0; i<=10000000 ; i++) ;
          cout<<s[j];

}
Last edited on
That doesn't introduce any delay on most compilers (actually, every compiler I just tested): the empty loop does nothing and is removed.

On those compilers where it could introduce a delay, it does not print one letter at a time because there are no calls to cout.flush().

Here's a program that does:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>
#include <thread>
#include <chrono>

int main()
{
    std::string s="hello";
    for(char c: s)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cout << c << std::flush;
    }
}
Last edited on
Cubbi


You can change 10000000 because it's very fast
write 19999999 and see what will happen

 
for(int i=0; i<=10000000 ; i++) ;


write => for(int i=0; i<=19999999 ; i++) ;
write 19999999 and see what will happen

It doesn't matter what number you use. The loop does nothing and is removed by the compilers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;



void main()
{
    string s="Last Assignment\n";
	for(int j=0; j< s. size( ) ; j ++)
	{
		for(int i=0; i<=19999999 ; i++) ;
		cout<<s[j];
	}

}



Its work fine !!!!!!!
Try turn on optimizations and see if it still works "fine".
@Ali93 it works but its not a very good way of doing it looping so many times is very inefficient
I see
Now i get it, thanks for the explanation

I'm not pro like you guys , i don't care about the details

I have an assignment and i add this to make it more attractive at the end of my code.

Thanks again :)
What he means is: Once you build your code in Release mode, that loop will be removed thanks to optimizations. You should use Win32's Sleep, or build your own with clock().
I have an assignment and i add this to make it more attractive at the end of my code.


Word of Warning: Silly little bells & whistles to make your console programs output look cute are generally to be avoided (unless you're writing the code for children or something). Most real users will just be annoyed or think your code has bugs. Once you get into GUI programming you can use all the pretty fonts, colors and graphics. In console programs it's better to just concentrate on writing good code.
Topic archived. No new replies allowed.