Results 1 to 6 of 6

Thread: Updating text on the current line..

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    109

    Updating text on the current line..

    OK.. this is a console app...

    -------------
    #include <stdio.h>
    #include <iostream.h>

    void main(void)
    {
    int i=0;

    printf( "%i", i );

    for( i=0; i<10; i++ )
    {
    // update %i with value of i
    }
    }
    -------------

    now i want to have it count to 10... but just update the current line where the printf is printing i (like it will show 1, then replace 1 with 2, then replace 2 with 3, etc etc...) .... but i dont know how to update the current line..... anyone know how?

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    PHP Code:
    #include <stdio.h> 
    #include <iostream.h> 

    void main(void

        
    int i=0
        
        
        for( 
    i=0i<10i++ ) 
        { 
            
    // update %i with value of i 
            
    printf"%i"); 
        }
        
    cin>>i;

    It will only count from 0 to 9 but you can make it count up to 10 by changing the for loop header to:
    PHP Code:
    for( i=0i<=10i++ ) 
    Baaaaaaaaah

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    109
    that just prints out 0123456789 and then inputs a #..

    i need it to keep updating the number on the screen...

    so when the program is counting, all you will see is the current number.

    ... you know how when some programs run, and show their percent done....

    like say this is the line printed to the console.....

    "Copying files, 10% complete"


    ... how do they keep updating the percent on the current line?

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    PHP Code:
    #include <stdio.h> 
    #include <iostream.h> 
    void main(void

        
    int i=0
        
        
        for( 
    i=0i<=10i++ ) 
        { 
            
    // update %i with value of i 
            
    printf"%i"); 
            
    printf("\\b");
            
        }
        
    cin>>i;

    You may also need some kind of delay to keep the number stayed on the screen for some time.
    Last edited by abdul; May 6th, 2002 at 11:53 PM.
    Baaaaaaaaah

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And you need to determine the number of digits i has because you must output \b that many times...

    Or output this string:
    "\r \r"
    Put as many spaces between the two \r as you have digits at maximum (currently there are 5)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    109
    thx .. ya i was just told about \r from my friend..

    but then i went into wondering further how you would update text on a different line.... so i finally found the console functions in the msdn.. so i wrote up a little sample program .... ill paste it below in case anyone was wondering how to do it..

    Code:
    #include <windows.h>
    
    void main(void)
    {
    	HANDLE hScreenBuf;
        COORD coordBufSize; 
        COORD coordBufCoord;
    	DWORD dwRead;
    	char buffer[100];
    	char chBuffer[100] = "This is my text";
    	
    	// create a new screen buffer for reading and writing
    	hScreenBuf = CreateConsoleScreenBuffer(
    		GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    	
    	// set the screen buffer size (50 columns x 10 rows)
    	coordBufSize.X = 50;
    	coordBufSize.Y = 10;
    	SetConsoleScreenBufferSize(hScreenBuf, coordBufSize);
    
    	// set new screen buffer as the active screen buffer
    	SetConsoleActiveScreenBuffer(hScreenBuf);
    	
    	// set the cursor at position (10,3) on the screen buffer
    	coordBufCoord.X = 10;
    	coordBufCoord.Y = 3;
    	SetConsoleCursorPosition(hScreenBuf, coordBufCoord);
    
    	// print chBuffer to the screen buffer (at the position we set earlier)
    	WriteConsole(hScreenBuf, chBuffer, strlen(chBuffer), 0, NULL);
    
    	// reset pointer at position (10,3) so we can read what we just printed
    	coordBufCoord.X = 10;
    	coordBufCoord.Y = 3;
    
    	// read from screen buffer and put into buffer variable
    	ReadConsoleOutputCharacter(
    		hScreenBuf, &buffer[0], strlen(chBuffer), coordBufCoord, &dwRead);
    	
    	// null terminate the string
    	buffer[dwRead] = '\0';
    
    	// show what was read from screen buffer in a message box
    	MessageBox(NULL, buffer, "", NULL);
    	
    	// set stdout (standard output) back to the active screen buffer
    	SetConsoleActiveScreenBuffer(GetStdHandle(STD_OUTPUT_HANDLE));
    	// close the screen buffer object we created
    	CloseHandle(hScreenBuf);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width