Sleep

so this seems like it should be easy to solve, I'm just not sure how to approach the problem. basically I want to be able to simplify my code:

#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

int main(){
Sleep(100);
cout<<"H";
Sleep(100);
cout<<"e";
Sleep(100);
cout<<"l";
Sleep(100);
cout<<"l";
Sleep(100);
cout<<"o";
Sleep(100);
cout<<"!";
getch();
return 0;
}

I want each letter to be typed with a delay of Sleep(100);
I know there has to be a way to form that into one easy command, instead of doing it the way I just did in my example. Help is greatly appreciated :)
You could use an array and for loop.
Hope this helps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void z() {Sleep(500);}

char x[6] = {'h','e','l','l','o','!'};

int main(){
    for (int i=0;i<6;i++)
    {
        cout << x[i];
        z();
    }
getch();
return 0;
}

thanks wachtn, thats definitely improvement. However, is it possible to improve it some more? For example, if I would want to have C++ write a few sentences, I think that the current improvement will still take a while because you have to write each letter individually with ' on both sides.
Do you know how to output data to a file using <fstream> ? Writing programs to generate repetitive code is a great trick and its easy to learn.

example
1
2
3
4
5
6

for (int i ...)
{ 
    a_file << "'" char[i] << "'"
}
// The program will write itself.  

Last edited on
I haven't ever used fstream in particular (however, I have messed around with ostringstream if thats similar), but hey, I guess now is a good time to start learning ;)
http://www.cprogramming.com/tutorial/lesson10.html

It really is a great trick. One of the first programs I wrote was a trivia game. Writing a program to generate the question function saved me a sh(hh this is a family site) lot of typing.
sweet, thanks! I'll definitely look into that. (well, as long as I keep finding motivation to keep teaching myself haha)
Keep at it. I gave up learning to program because I never thought I would have a use for it. Im only a beginner in C++ but in the last month I wrote 2 simple programs that could end up saving me a lot of time. Its a great feeling you get when you teach a computer to do your job for you.
Last edited on
Sweet, I changed something from the example you gave me, and now it works pretty well :)

#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void z() {Sleep(50);}

char x[] = "Hello! This program writes each letter after 0.05 seconds. And it finally works :)";

int main(){
for (int i=0;i<81;i++)
{
cout << x[i];
z();
}
getch();
return 0;
}

the only problem is that you have to know how many chars you have for i, but I can live with that for now ;)
Easy fix. Change the for loop like so. Now no matter how long the txt your program will end where it should.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void z() {Sleep(50);}

char x[] = "Hello! This program writes each letter after 0.05 seconds. And it finally works :)#";

int main(){
for (int i=0;x[i]!='#';i++)
{
cout << x[i];
z();
}
getch();
return 0;
}

sweetness, thanks man!
Was that # character specially introduced to mark the last character in the string. If so why?

The string is terminated with a 0 (zero value) char. Why not use that??
alternatively, individual characters of a string can be accessed using the array[] brackets. Wouldn't it be simpler to make x a string and use
 
for(int i=0;i<length.x();++i)
i just edited it again, and this seems to be working fine.

for (int i=0; i<sizeof(x);i++)

seems a bit easier, but maybe that's just me ;)
Last edited on
That is easy. thats for the advice everyone.
I would consider putting the loop in a function that takes a string.
Topic archived. No new replies allowed.