Typewriter effect

Typewriter effect for a unix system (mac)

Page 1 of 1

4 Replies - 4166 Views - Last Post: 28 November 2009 - 08:23 PM Rate Topic: -----

#1 Jud202  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 02-March 09

Typewriter effect

Post icon  Posted 28 November 2009 - 04:11 PM

Hey. Just wondering if there is a way to do a typewriter effect on a mac.

I've seen lots of ways to do it on windows, like:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <windows.h>
using namespace std;

int main() {

	 char myStory[] = {
		  "This is my story\nThis is the second line\nok bye!\n"
	 };

	 for (unsigned int x = 0; x < strlen(myStory); x++) {
		  cout << myStory[x];
		  sleep(200);
	 }

	 system("pause");
	 return 0;
}


but of course stdafx.h, windows.h are not available for a Unix system. Any ideas?
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Typewriter effect

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Typewriter effect

Posted 28 November 2009 - 06:07 PM

Take out the non standard junk, go from there. The standard function usleep should work.

#include <iostream>

using namespace std;

void slowPrint(unsigned long speed, const char *s) {
	int i = 0;
	while(s[i]!=0) {
		cout << s[i++];
		cout.flush();
		usleep(speed);
	}
}

int main() {
	slowPrint(200000, "This is my story\nThis is the second line\nok bye!\n");
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 Jud202  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 02-March 09

Re: Typewriter effect

Posted 28 November 2009 - 07:30 PM

Awesome! That works perfectly! Thank you
Was This Post Helpful? 0
  • +
  • -

#4 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 551
  • Joined: 24-July 08

Re: Typewriter effect

Posted 28 November 2009 - 08:16 PM

When you get deeper into this cross-platform kindof stuff, you will need to get familar with macros. you can nest a macro in your code to use part of a code if compiled in linux and visa versa. So you can write one code and have it work on multiple platforms....

Happy coding =)
Was This Post Helpful? 0
  • +
  • -

#5 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: Typewriter effect

Posted 28 November 2009 - 08:23 PM

View PostUG Cyber, on 28 Nov, 2009 - 07:16 PM, said:

When you get deeper into this cross-platform kindof stuff, you will need to get familar with macros. you can nest a macro in your code to use part of a code if compiled in linux and visa versa. So you can write one code and have it work on multiple platforms....

Happy coding =)


Or meta programming. pre processor tags can accomplish splitting code quite nicely.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1