Thread: Is there a quicker way to do this?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    51

    Is there a quicker way to do this?

    I'm trying to make a typing animation in my C program, but it's taking forever because I have to use the sleep() function after every letter. Writing something long seems impossible. If I have to I'll do it this way, but I'd rather not.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
          printf ("T");
          sleep(100);
          printf ("h");
          sleep(100);
          printf ("i");
          sleep(100);
          printf ("s ");
          sleep(100);
          printf ("i");
          sleep(100);
          printf ("s ");
          sleep(100);
          printf ("a ");
          sleep(100);
          printf ("t");
          sleep(100);
          printf ("e");
          sleep(100);
          printf ("s");
          sleep(100);
          printf ("t");
          getchar();
          }
    Last edited by PYROMANIAC702; 07-28-2011 at 10:42 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a loop
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Define "taking forever". You mean for you to type the code? Why not use a loop, with the message in a string/char array?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by laserlight View Post
    Use a loop
    I could do that, but I don't understand how the loop would work.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by tabstop View Post
    Define "taking forever". You mean for you to type the code? Why not use a loop, with the message in a string/char array?
    By "taking forever" I did mean for me to type the code.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by PYROMANIAC702 View Post
    I could do that, but I don't understand how the loop would work.
    Well there is no time like the present to learn. Try reading Tutorial 3: Loops

    This is becoming quite the pattern of wanting to be spoon fed the C language one item at a time...............
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by PYROMANIAC702 View Post
    I could do that, but I don't understand how the loop would work.
    Loops tend to work by repeating all those repetitive statements for you. You have a print, and a sleep, then a print, then a sleep, then a print, then a sleep, then a print, then a sleep, etc. So put the print and the sleep in a loop and you're done.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by AndrewHunter View Post
    Well there is no time like the present to learn. Try reading Tutorial 3: Loops

    This is becoming quite the pattern of wanting to be spoon fed the C language one item at a time...............
    When I said I don't understand how the loop would work, I meant I don't know how to implement the loop for my situation. I know how to do a loop. The part I don't understand is how to go from one letter to another in the loop.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by PYROMANIAC702 View Post
    When I said I don't understand how the loop would work, I meant I don't know how to implement the loop for my situation. I know how to do a loop. The part I don't understand is how to go from one letter to another in the loop.
    There's this magic operator called ++ you may want to look into. You want to print the 0th letter. Then you want to print the 1st letter. Then you want to print the 2nd letter. Then you want to print the 3rd letter.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by PYROMANIAC702 View Post
    When I said I don't understand how the loop would work, I meant I don't know how to implement the loop for my situation. I know how to do a loop. The part I don't understand is how to go from one letter to another in the loop.
    Oh, well in that case you should read Lesson 8 - arrays

    I am sure you will find your answer there. BTW feel free to read all the other tutorials too, plenty of good information.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    Quote Originally Posted by tabstop View Post
    There's this magic operator called ++ you may want to look into. You want to print the 0th letter. Then you want to print the 1st letter. Then you want to print the 2nd letter. Then you want to print the 3rd letter.
    I don't know how to explain this, but I'll try. Say a = 0 and then I did a++, a would go up 1 every cycle in the loop. That is with an integer. I have no clue on how to get it to go up one letter every time though. Like for it to go from T to h to i to s. What would tell it to do that?

    EDIT: I'll read that now, Andrew. I forgot to refresh.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How do you reference the 0th letter of a message?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by PYROMANIAC702 View Post
    When I said I don't understand how the loop would work, I meant I don't know how to implement the loop for my situation. I know how to do a loop. The part I don't understand is how to go from one letter to another in the loop.
    What is a string? It's an array of characters... Arrays are accessed by indexes...
    What's a loop? It's a repeated function that uses indexes to control it's position and exit.

    See a connection there?

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The logic is easy:
    Code:
    for(each letter in the string) {  //and i++ after each loop
      print(the next letter;   //stringArray[i]
      sleep(1);
    }

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by PYROMANIAC702 View Post
    it's taking forever because I have to use the sleep() function after every letter.
    Sleep less.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there a quicker way to initialise an array?
    By webznz in forum C Programming
    Replies: 5
    Last Post: 10-25-2007, 03:45 AM
  2. Why a quicker shutdown?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-02-2007, 04:45 PM
  3. quicker access to structmembers?
    By darksaidin in forum C++ Programming
    Replies: 14
    Last Post: 07-29-2003, 09:14 AM
  4. quicker for inserts
    By the Wookie in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2003, 12:16 PM
  5. What is Quicker
    By Nor in forum Game Programming
    Replies: 3
    Last Post: 02-02-2002, 04:29 PM