CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2003
    Posts
    53

    Display current time in command prompt with update

    I would like to know how I display the current type in the command prompt so that it updates itself to show the changing time.

    Thanks.

  2. #2
    Join Date
    Feb 2003
    Posts
    53
    bump...

    Still need an answer...

  3. #3
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    using windows?

    Code:
    int main()
    {
    	while( true )
    	{
    		SYSTEMTIME SystemTime;
    		GetSystemTime( &SystemTime );
    
    		system("cls");
    		cout << SystemTime.wHour << ":" << SystemTime.wMinute << "." << SystemTime.wSecond;
    		Sleep( 1000 );
    	};
    
    	return 0;
    }

  4. #4
    Join Date
    Feb 2003
    Posts
    53
    What library are you using?

    I know it is not <ctime>.

  5. #5
    Join Date
    Jun 2002
    Posts
    1,417
    Using mwilliamson's idea, here is another method. This one just moves the cursor back to the beginning of the current line without erasing the entire window.

    Code:
    int main(int argc, char* argv[])
    {
    	while( true )
    	{
    		SYSTEMTIME SystemTime;
    		GetSystemTime( &SystemTime );
    
    		cout << "\r" << SystemTime.wHour << ":" << SystemTime.wMinute << "." << SystemTime.wSecond;
    		Sleep( 1000 );
    	};
    
    	return 0;
    }

  6. #6
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    If you do that, when you switch from 12:59.59 to 1:00.00 then you will not overwrite all of the characters making your output 1:00.009.

  7. #7
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    #include <windows.h>

  8. #8
    Join Date
    Jun 2002
    Posts
    1,417
    Originally posted by mwilliamson
    If you do that, when you switch from 12:59.59 to 1:00.00 then you will not overwrite all of the characters making your output 1:00.009.
    that's easy to fix. just add some more spaces at the end
    Code:
    int main(int argc, char* argv[])
    {
    	while( true )
    	{
    		SYSTEMTIME SystemTime;
    		GetSystemTime( &SystemTime );
    
    		cout << "\r" << SystemTime.wHour << ":" << SystemTime.wMinute << "." << SystemTime.wSecond << "    ";
    		Sleep( 1000 );
    	};
    
    	return 0;
    }

  9. #9
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    thats a mess... lets just use cls

  10. #10
    Join Date
    Jun 2002
    Posts
    1,417
    if you use cls you erase the entire screen -- there may be other stuff on the screen that you don't want erased. it would also cause a flicker affect.

  11. #11
    Join Date
    Feb 2003
    Posts
    53
    Thanks guys!

    I was trying to time the execution of a program using constant time update. That won't work in the DOS console however.

    I will simply display the time before and after execution of the code.

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

Featured