Thread: C Type Writer Effect help please.

  1. #1
    Registered User
    Join Date
    Feb 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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and what do you call type writer effect?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  5. #5
    Registered User
    Join Date
    Feb 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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 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