loops

This is a discussion on loops within the C Programming forums, part of the General Programming Boards category; Most of the basic concepts in C I understand. Some of them I have yet to type a small source ...

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    loops

    Most of the basic concepts in C I understand.
    Some of them I have yet to type a small source off the top of my head without "cheating" though.

    In this loop if there's anything wrong I could make it right by referring to a document, i'm just testing myself.
    Is it right?
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
    	int counter;
    	char * message = "This is a message."
    			 "\nThis message types itself.";
    	for(counter=0;counter<45;counter++)
    	{
    		printf("%c", message[counter]);
    		sleep(150);
    	}
    	return 0;
    }
    The world is waiting. I must leave you now.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    That sleep() function, where's that from? Are you on Unix? If so, you'll probably find that is a 150 second delay (2 and a half minutes). There isn't one in time.h (afaik), on Unix it's in unistd.h

    You may be thinking of the windows.h version (Sleep() ), which gives you 150 milli seconds (I think). This makes more sense.

    Also, I compiled it under 2 different compilers. When under gcc, the output buffer didn't flush until the newline character was reached, whereas bcc flushed after each character. Maybe you'd need to fflush(stdout) each iteration.

    And if you're mucking about with loops, why not try the pointer version (saves counting the characters in the string):
    Code:
    char * message = "This is a message.\nThis message types itself.";
    char *ptr;
    	
    for(ptr = message; *ptr != '\0'; ptr++)
    {
    	printf("%c", *ptr);
    	Sleep(150);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    That sleep() function, where's that from? Are you on Unix? If so, you'll probably find that is a 150 second delay (2 and a half minutes). There isn't one in time.h (afaik), on Unix it's in unistd.h

    You may be thinking of the windows.h version (Sleep() ), which gives you 150 milli seconds (I think). This makes more sense.
    Ah yes, goof up.

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int hm(int mili);
    
    int main()
    {
    	int counter;
    	for(counter=0;counter<100;counter++)
    	{
    		hm(counter);
    	}
    	return 0;
    }
    
    int hm(int mili)
    {
    	char * message = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz\n";
    	char * ptr;
    	for(ptr = message; * ptr != '\0'; ptr++)
    	{
    		printf("%c", * ptr);
    		Sleep(mili);
    	}
    	return 0;
    }
    Useless, silly, and even not worth waiting to see the end.
    The alphabet 100 times in a row!
    Each time, it takes longer!...NOOOO!

    I think I got loops down...
    I was REALLY confused on loops, any kind.
    For the longest time, while, if....nothin'
    Also, "communicating between functions" in any form.
    I couldn't figure that out. Now I'm getting them down pretty good.
    About time too, both are very powerful and useful .
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 11:09 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM

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