Help with output delays

Hey c++,
I'm trying to write a code so that text is outputted one letter at a time (every 50 milliseconds or so - think of the "pokemon" style text). I am using Ubuntu and am quite new at programming (started early march) so I don't know all that much. But some help with explanation would be muchly appreciated.

-D
1
2
3
4
5
6
7
void pokeprintf(const char * de){
unsigned int ns = strlen(de);//we use this to find how long the string is.
    for(unsigned int i =0;i<ns;i++){
	putchar(de[i]);//we use this to print just the current character from the string.
	fflush(stdout);//we use fflush to print the character to the screen now.
	usleep(20000);//we use usleep to wait 20000 micro seconds or 20 milliseconds.
}

this is what I used in a small Zork clone i made to get a similar effect.
You'll have to include <unistd.h> and <string.h> though.
With just a little modification, this could also be used just like printf to print formatted data as well, but if you're a beginner, it might be better to wait a little bit until learning va_list's.
Last edited on
or you can keep it simple
1
2
3
4
std::string str;
std::cout << "Please enter a string: " << std::flush;
std::getline(std::cin, str);
for(unsigned int i; i<str.size(); i++) { std::cout << str[i] << std::flush; sleep(.002); }
Last edited on
Topic archived. No new replies allowed.