Printing a string with a time delay

This is a discussion on Printing a string with a time delay within the C++ Programming forums, part of the General Programming Boards category; is there a way to make the characters in a string to print off 1 by 1, with a random ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question Printing a string with a time delay

    is there a way to make the characters in a string to print off 1 by 1, with a random time delay between the showing each of the letters, I can already do the random part, so that isnt the problem, the problem is making the letters show each other 1 by 1, and that is where I need a little help...

  2. #2
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    actually, on second thoughts, I realised how simple it is, and how stupid the question is, I thought of a way round the problem, even though im sure that it isnt the most efficient way to do it...


  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Here's one way using iterators:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    int main()
    {
      typedef std::string::const_iterator iter;
    
      std::string msg ( "This is a message" );
      iter begin = msg.begin();
      iter end = msg.end();
    
      for ( iter it = begin; it != end; ++it ) {
        std::cout.put ( *it );
        Sleep ( std::rand() / ( RAND_MAX / 1000 ) );
      }
    
      std::cout<<std::endl;
    }
    And another using indices:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    int main()
    {
      typedef std::string::size_type iter;
    
      std::string msg ( "This is a message" );
      iter end = msg.size();
    
      for ( iter it = 0; it != end; ++it ) {
        std::cout.put ( msg[it] );
        Sleep ( std::rand() / ( RAND_MAX / 1000 ) );
      }
    
      std::cout<<std::endl;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  4. Time Delay
    By dragonklown in forum C++ Programming
    Replies: 12
    Last Post: 02-10-2005, 06:18 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

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