Thread: typing effect on text

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    41

    typing effect on text

    Hey, how can i have text that appears from cout appear as though it is being typed, i know its possible and im just wondering if anyone has any ideas as to how, or if i should use a differnt input of text other than cout to make it work, thanx

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    like this?

    Code:
    for(int i = 0; ch[i]; i ++)
    {
         cout << ch[i];
         Sleep(50);     // from windows.h
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    41
    that should do the trick, thanx
    In order of learned:
    HTML - Mastered
    CSS - Enough to use
    SSI - Mastered
    PHP - Advanced
    C/C++ - Current Project

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    41
    ok, im not to use to using the for loop, so could you perhaps explain just a bit more as to how i should set it up..
    In order of learned:
    HTML - Mastered
    CSS - Enough to use
    SSI - Mastered
    PHP - Advanced
    C/C++ - Current Project

  5. #5
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Code:
    char ch[6] = "HELLO";
    
    for(
    int i = 0; // the loop's declaration
    ch[i]; // this checks for if the char still has content 
    i ++ // increment the placeholder
    )
    {
         cout << ch[i]; // output the current character
         Sleep(50);     // wait 50 milisecs
    }
    Hope that helps!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    41
    it does, and i get what how to use the for now but im wondring, how can i use it to do it to a string of text, (with whitespace - e.g. "Welcome to earth") lol, thanx again
    In order of learned:
    HTML - Mastered
    CSS - Enough to use
    SSI - Mastered
    PHP - Advanced
    C/C++ - Current Project

  7. #7
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Code:
    string str = "Welcome to Earth!";
    
    for(int i = 0; str[i] != '\0'; i ++)
    {
         cout << str[i];
         Sleep(50);
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    41
    Great, it works perfect, thanx a bunch!!!
    In order of learned:
    HTML - Mastered
    CSS - Enough to use
    SSI - Mastered
    PHP - Advanced
    C/C++ - Current Project

  9. #9
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    np
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    what if i have two strings? how will it jump to the second string?

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> for(int i = 0; str[i] != '\0'; i ++)
    alternatively -
    Code:
    string stringinstance = "This is a string";
    for ( int i=0; i<stringinstance.size(); i++ ) cout<< stringinstance[i];
    Do you want them to both type at the same time, or one after another? If you want them to display one afte another, I'd recommend making a function -

    Code:
    void TypeText ( stiring text )
    {
       for ( int i=0; i<(int)text.size(); i++ )
       {
          cout<< text[i];
          Sleep( 50 );
       }
    }
    then just call that in main

    Code:
    int main( void )
    {
       TypeText( "Number one" );
       TypeText( "Numbertwo" );
    
       return 0;
    }
    Last edited by twomers; 09-02-2006 at 08:09 AM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    one after another.

    here is my code. it only display the first string

    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    #include<dos.h>

    void main() {
    clrscr();
    char msg[] = "This is only a test.";
    char msg1[] = "I hope that this will work.";
    for (int i = 0; msg[i] != '\0'; i++) {
    cout << msg[i]; delay(100);
    }
    getch();
    }

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    #include<iostream>
    // #include<conio.h> not standard 
    #include<string>
    
    #include<windows.h> // for Sleep
    
    using std::string;
    using std::cout; //  or using namespace std; for both of those
    
    void TypeText ( stiring text )
    {
       for ( int i=0; i<(int)text.size(); i++ )
       {
          cout<< text[i];
          Sleep( 50 );
       }
    }
    
    int main()
    {
    	string msg = "This is only a test.";
    	string msg1 = "I hope that this will work.";
    
    	TypeText ( msg );
    	TypeText ( msg1 );
    
    
    	return 0;
    
    }
    I've changed things you should change to 'Sienna' for some reason. I'd recommend my function. void main = bad. getch() to stop the program = not the best method, check out the FAQ, use std::strings instead of char[]'s
    Oh dear

    Oh, and USE CODE TAGS!!
    Last edited by twomers; 09-02-2006 at 08:20 AM.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    ok. thank you for your help.

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Ok, you might want to cout<< "\n"; or something between the function calls to make them go on different lines.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  2. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  3. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM