Problem with dissecting strings

What I want to do: a ghostwriter function that takes in a string and slowly prints out each letter with a small pause in between. as if someone is actually sitting behind a keyboard and manually typing out each letter.

The problem is that it cannot be compiled because "string is out of bounds".



My function:

1
2
3
4
5
6
7
8
9
void ghosting (string s1, int t1) {

	int len = sizeof(s1); 

	for (int i=0; i<len; i++){
	cout << "" << s1[i]; 
	Sleep(t1);
	}
}




please help and thanks muchos!!!
Last edited on
You are using sizeof incorrectly in this case. sizeof gives you the amount of bytes the variable takes up in memory (which is 32 on my computer). It doesn't matter how many characters are inside of it.

You want to use s1.size() to get the number of characters.
thanks james. it works perfectly now.
Topic archived. No new replies allowed.