C++ Typewriter text effect (xcode)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

C++ Typewriter text effect (xcode)

Post by TheThirdL3g »

I am making a console app (using a mac and xcode), and I wanted to add a typewriter effect to text. I thought it would be a pretty simple task, but when I run the app the console freezes when it reaches the function. It still accepts user input, but it doesn't continue.

Code: Select all

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

string ptext;
void twtext(char[]);

int main (int argc, char * const argv[]) {
    twtext("Typewriter effect test\n");
	
	cin >> ptext;
    return 0;
}

void twtext(char text[]) {
	for (int i = 0; i < strlen(text); i++) {
		cout << text[i];
		sleep(200);
	}
}

Any help would appreciated.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: C++ Typewriter text effect (xcode)

Post by M_D_K »

that's cause sleep() takes seconds not milliseconds. you'd need to use something like nanosleep
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

Re: C++ Typewriter text effect (xcode)

Post by TheThirdL3g »

Thanks for the help. Nanosleep worked for delaying, but it for some reason when the timer is up instead of only printing one letter it prints the whole line of text.

Even when I use a while loop for a delay it does the same thing.

The code is showing the use of the while loop with the nanosleep method commented out.

Code: Select all

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

string ptext;
void twtext(char[]);
void msleep(int);

int main (int argc, char * const argv[]) {
    twtext("Typewriter effect test\n");
	
	cin >> ptext;
    return 0;
}

void twtext(char text[]) {
	for (int i = 0; i < strlen(text); i++) {
		cout << text[i];
		int x = 0;
		while (x < 9999999) {
			x++;
		}
		//msleep(200);
	}
}

void msleep(int ms) {
	struct timespec time;
	time.tv_sec = ms / 1000;
	time.tv_nsec = (ms % 1000) * (1000 * 1000);
	nanosleep(&time,NULL);
}
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: C++ Typewriter text effect (xcode)

Post by M_D_K »

I forgot to mention that you're not flushing stdout this can be done using std::endl. BUT that would also create a new line what you want to do is use fflush(stdout) which is in cstdio

Code: Select all

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>

using namespace std;

string ptext;
void twtext(char[]);
void msleep(int);

int main (int argc, char * const argv[]) {
    twtext("Typewriter effect test\n");
   
   cin >> ptext;
    return 0;
}

void twtext(char text[]) {
   for (int i = 0; i < strlen(text); i++) {
      putchar(text[i]);
      fflush(stdout);
      msleep(200);
   }
}

void msleep(int ms) {
   struct timespec time;
   time.tv_sec = ms / 1000;
   time.tv_nsec = (ms % 1000) * (1000 * 1000);
   nanosleep(&time,NULL);
}
Also if you want it to look more like a person is typing you could throw a little randomness into the delay (not much).
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Post Reply