Problems on making a class with this code.

Hi, I found this code online that basically couts a string's characters one by one.
I'm having some problems on making it a class where I just set the string and keep the "Typewriting" effect so that I don't have to re-write it all over again whenever I use it.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
string hello = "Phrase";
int x=0;
while ( hello[x] != '\0')
{
	cout << hello[x];
	Sleep(500);
	x++;
};
}

Could you help me? Thanks
Last edited on
Why do you want to use a class for this?
A simple function should do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void typewriter(const string& s, DWORD delay)
{
  for (char ch: s)
  {
    cout << ch;
    Sleep(delay);
  }
}

int main()
{
  typewriter("Hello World", 500);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void typewriter(const string& s, DWORD delay)
{
  for (char ch: s)
  {
    cout << ch;
    Sleep(delay);
  }
}

int main()
{
  typewriter("Hello World", 500);
}


I don't know why but this one just keeps typing "W" every 0.5 seconds, how can I fix it?
Very strange. It prints the whole message you my machine.
What compiler / IDE do you use?
Topic archived. No new replies allowed.