Outputing Slowly

So, I know how to make the text in a string read out slowly but my question is, is there a faster way to do it or will I have to go through every sentence in my program and loop every single one? Here's an example of the loop I'm using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
     string a = "Hello World!";

     for(int j = 0; j < a.size(); j++)
     {
          for(int i = 0; i <= 18000000; i++);
          cout << a[j];
     }
return 0;
}


Any help is greatly appreciated. If this is the best way to do it then I guess I'll suck it up and start coding. lol
Hi,

I see that you are trying to create the real time typewriting effect
but

instead of using an empty for loop and frying your CPU you could use sleep functions

http://www.cplusplus.com/reference/thread/this_thread/sleep_for/

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx
That's a much better idea but the loop set up would be somewhat similar though, yes? Is there a faster way than looping or is that my best approach. Thanks for the advise. I didn't even thinking about what the empty loop would do to my CPU.
That's a much better idea but the loop set up would be somewhat similar though, yes?

um.. yes I suppose

Is there a faster way than looping or is that my best approach.

IDK... really

Thanks for the advise.

Welcome :)


I didn't even thinking about what the empty loop would do to my CPU.

Same here

further reading:
http://www.cplusplus.com/forum/general/141544/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
     string a = "Hello World!";

     for(int j = 0; j < a.size(); j++)
     {
          Sleep(50);
          cout << a[j];
     }
return 0;
}


Welp, it's faster and more efficient. I don't think I'm going to find a faster way to do that real time type writing effect. Thanks again.
welcome :)

PS: use this function to make your code smaller if you want to type more

1
2
3
4
5
6
7
8
void type(string a)
{
	 for(int j = 0; j < a.size(); j++)
     {
          Sleep(50);
          cout << a[j];
     }
}


now, you wont have to use the complicated loop again and again and just the function

eg.:

1
2
3
type("Hello World");
type("Hello typewriter effect.........");
//and so on...... 
haha, I was just about to mention that actually. Great minds think alike.
Topic archived. No new replies allowed.