stay in place

This is a discussion on stay in place within the C Programming forums, part of the General Programming Boards category; how can i get this program to not move around like it does? i want it to stay in one ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    14

    stay in place

    how can i get this program to not move around like it does? i want it to stay in one spot so it looks like the little thing is spinning.
    PHP Code:
    #include    <stdio.h>

    void main()
        {
        
    long counter1,counter2,towait;
        
    char anim[5]="|\\-/";
        for(
    counter1=0;counter1<10000;counter1++)
            {
            for(
    counter2=0;counter2<5;counter2++)
                {
                
    printf("%c",anim[counter2]);
                for(
    towait=0;towait<15000000;towait++)
                    {
                    continue;
                    }
                }
            }
        } 

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int clrscr();
    
    int main()
    {
        long counter1,counter2,towait;
        char anim[5]="|\\-/";
        for(counter1=0;counter1<10000;counter1++)
            {
                    for(counter2=0;counter2<5;counter2++)
                {
                    clrscr(); /* clear screen, change it prefered */
                    printf("%c",anim[counter2]);
                    for(towait=0;towait<15000000;towait++)
                    {
                    continue;
                    }
                }
            }
    }
    
    int clrscr()
    {
        	COORD coordScreen = { 0, 0 };
        	DWORD cCharsWritten;
        	CONSOLE_SCREEN_BUFFER_INFO csbi;
        	DWORD dwConSize;
        	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
        	GetConsoleScreenBufferInfo(hConsole, &csbi);
        	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        	FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
        	GetConsoleScreenBufferInfo(hConsole, &csbi);
        	FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
        	SetConsoleCursorPosition(hConsole, coordScreen);
        	return 0;
    }
    Does this help?

    - edit -
    Clear the screen after you print a character, this will simulate the spinning effect. Also, your "\" needed to be doubled "\\" otherwise the compiler will complain. My compiler didn't like void main either. To make things really pretty you could kill the cursor...
    The world is waiting. I must leave you now.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    14
    there isn't a simpler way? i was hoping to not double the size of the program just for that little thing.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    What are you compiling on?
    If you're on Windows, replace the clrscr() with system("cls");. Also, remove the clrscr() function as you're not calling it anymore. You can call the screen clearing options of the operating system you're running on. Otherwise, the only way to make that thing seem like it's spinnnig is by creating an extra function to make sure each character gets drawn in the same exact spot.

    Simpler way? Size of the code. Messing around with your program I came up with this:
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    void gotoxy( int x, int y );
    void clrscr(void);
    
    int main()
    {
        	long counter1,counter2,towait;
        	char anim[5]="|\\-/";
        	clrscr();
        	for(counter1=0;counter1<10000;counter1++)
            {
            	for(counter2=0;counter2<5;counter2++)
                	{
                    	gotoxy( 0, 0 ); /* clear screen, change it prefered */
                    	printf("%c",anim[counter2]);
                    	printf("\n\nHmmm...");
                    	for(towait=0;towait<15000000;towait++)
                    	{
                    		continue;
                    	}
                	}
            }
    }
    
    void gotoxy ( int x, int y )
    {
      	COORD coord;
      	CONSOLE_CURSOR_INFO c = { 1, 0 };
      	coord.X = (short)x;
      	coord.Y = (short)y;
    	SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
      	SetConsoleCursorInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ), &c );
    }
    
    void clrscr(void)
    {
        	COORD coordScreen = { 0, 0 };
        	DWORD cCharsWritten;
        	CONSOLE_SCREEN_BUFFER_INFO csbi;
        	DWORD dwConSize;
        	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
        	GetConsoleScreenBufferInfo(hConsole, &csbi);
        	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        	FillConsoleOutputCharacter(hConsole, TEXT(' '), 
        	dwConSize, coordScreen, &cCharsWritten);
    	GetConsoleScreenBufferInfo(hConsole, &csbi);
    	FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
        	dwConSize, coordScreen, &cCharsWritten);
    	SetConsoleCursorPosition(hConsole, coordScreen);
    }
    I have decided that I like this best. There probably are several ways to do this that are much simpler. Who's to say that they are the correct way though?
    Last edited by Shadow; 05-11-2002 at 07:40 PM.
    The world is waiting. I must leave you now.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    1. It's int main(), not void main()
    This is strike 2

    2. Try
    printf("%c\b",anim[counter2]);

    Any you might want to look up delay(), sleep(), Sleep() etc for those delay loops you have

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    14
    1. It's int main(), not void main()
    This is strike 2
    strike two? and whats the difference between int and void? they both work fine
    2. Try
    printf("%c\b",anim[counter2]);
    that works great, thanks
    Any you might want to look up delay(), sleep(), Sleep() etc for those delay loops you have
    what header is that in? i cant find it

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Cicero

    strike two? and whats the difference between int and void? they both work fine
    void or int main()? Read it here

    Sleep()> If your on Windows OS, the function is in windows.h (or at least it is on my compiler).
    Last edited by Hammer; 05-12-2002 at 08:13 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Originally posted by Hammer

    void or int main()?
    Oh god, I can see this thread five pages long by tomorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal place range. simple help needed
    By Chris0724 in forum C Programming
    Replies: 8
    Last Post: 09-10-2008, 02:41 AM
  2. How to NOT reverse a string in place.
    By cdalten in forum C Programming
    Replies: 2
    Last Post: 01-12-2006, 08:06 AM
  3. A place to start - mac or pc?
    By GCat in forum C++ Programming
    Replies: 12
    Last Post: 11-19-2004, 11:21 AM
  4. make window stay in one place
    By space_ in forum Windows Programming
    Replies: 1
    Last Post: 07-12-2004, 02:07 PM
  5. Berlin: Searching for a place to stay
    By fabs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-14-2002, 01:18 AM

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