C Type Writer Effect help please.

This is a discussion on C Type Writer Effect help please. within the C Programming forums, part of the General Programming Boards category; I have been trying to create a simple type writer effect in c. Have not been successful. I figured it ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    C Type Writer Effect help please.

    I have been trying to create a simple type writer effect in c. Have not been successful.
    I figured it has to do with char and for, but I can't figure out how to do it.

    Maby someone on here can help me write a simple type writer effect in c. The kind you see in rpg and other games.

    Thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    and what do you call type writer effect?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    just simply print a single char of a string until the end of the string...
    put delay every time you print...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    
    int main()
    {
        char *eekkss = "wowowowowow";
        int len = strlen(eekkss);
        for (int i = 0; i <= len; i++){
            printf("%c", eekkss[i]);
            Sleep(800);
        }
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Also read about fflush() as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    I should of probable mentioned I am programming C on linux.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    
    int main()
    {
    	int i;
    	char *eekkss = "This is a test string.";
    	int len = strlen(eekkss);
    	for (i = 0; i <= len; i++){
    	printf("%c", eekkss[i]);
    	usleep(8000);
    	}
    }
    That is what I did, but I have a problem. It waits the whole 8000 microseconds then prints the whole string to the console. Instead of print 1 char usleep 8000, print another char usleep 8000. I will keep messing with it. Maby someone on here knows what is wrong with it.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Are you sure you could fill 8 ms interval?

    You could add fflush(stdout) to enforce printf output the char

    also note that eekkss[len] is nul-char that should not be printed

    char *eekkss - should be
    const char*
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Thanks. I got it to work ok for now.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    
    int main()
    {
    	int i;
    	const char *eekkss = "This is a test string.";
    	int len = strlen(eekkss);
    	for (i = 0; i <= len; i++){
    	printf("%c", eekkss[i]);
    	fflush(stdout);
    	usleep(15000);
    	
    	}
    
    }
    I will use usleep for now. Might use something else later in place of usleep.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Segmentation Fault?
    By John_L in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 08:37 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21