Thread: Displaying time in a dynamic fashion..how?

  1. #1
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85

    Question Displaying time in a dynamic fashion..how?

    Hello everybody..

    I'm working on a program in which i want to display time in the "hh:mm:ss"
    I used the follow method:
    Code:
    void showtime()
    {
      char datebuf[9];
      char timebuf[9];
    
      _strdate(datebuf);
      _strtime(timebuf);
    
      //display date
      cout << datebuf;
    
      //display time
      cout << timebuf;
    }
    But the problem is that i want the display to be dynamic, that is, i want to see the "ss" moving/incrementing just like a watch.
    How can i achieve this?
    Please can anyone help me, thanks!
    # If you want to be happy, think of others.
    # If you want to be miserable, think of yourself

    ~~~Team work is the best!~~~

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    one approach clear console screen

    Kuphryn

  3. #3
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    Quote Originally Posted by kuphryn
    one approach clear console screen

    Kuphryn
    How?? (my mind is overloading tright now )

    Note by
    i'm displaying this in graphics mode.
    Last edited by wakish; 07-03-2006 at 01:25 PM.
    # If you want to be happy, think of others.
    # If you want to be miserable, think of yourself

    ~~~Team work is the best!~~~

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    cls

    Kuphryn

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The FAQ contains an answer about clearing the console.

    But that's overkill. Just count the lengths of datebuf and timebuf and output that many backspace characters ('\b' or '\h', I can never remember). Do that at the end of the showtime() function. This means that the cursor position is still at the start of the line after the call. Whatever you output next will just overwrite what's already there.

    Wtih that in mind, you just need to call showtime() every second. That is actually the trickier task in a console program.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    any other method..please can you give an example of what you are trying to say?
    Thanks..
    # If you want to be happy, think of others.
    # If you want to be miserable, think of yourself

    ~~~Team work is the best!~~~

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I've an idea. Do you want the time to be at the same position all the time? If you do, you could throw in the following function:

    Code:
    void GotoXY  ( int X, int Y ) 
    { 
        COORD Cursor_Position; 
    
        Cursor_Position.X = X; 
        Cursor_Position.Y = Y; 
    
        SetConsoleCursorPosition( GetStdHandle ( STD_OUTPUT_HANDLE ), Cursor_Position ); 
    }
    which will go to a specific position in the console window. Then you'll want to clear the line, so you could just do this:

    Code:
    cout<< "                      ";
    where the number of spaces is defined by the number of characters which are on the screen.

    (or alternatively you could use setw() and setfill(), but if you use them, don't forget to hash include iomanip dot h.)

    then you do this:

    Code:
    cout<< '\r' << datebuf << " " << timebuf;
    The '\r' rewrites to the beginning of the line. Then you go merrily on your way. You may want to record the position of the cursor before you do the GotoXY though, cause you won't know where to go back to otherwise. I'll leave that research to you. Try google, or the search option on the forums.

  8. #8
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    nops..does not really work!
    # If you want to be happy, think of others.
    # If you want to be miserable, think of yourself

    ~~~Team work is the best!~~~

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Is this what you mean???? :
    Code:
    #include <iostream>
    #include <conio.h>
    #include <ctime>
    
    using namespace std;
    
    int key;
    char d[9], t[9];
    
    int main()
    {
    	for(;;)
    	{
    		if( ( key = _kbhit() ) != 0 )
    		{
    			break;
    		}
    		else
    		{
    			system("CLS");
    			_strdate(d);
    			_strtime(t);
    			cout<<"Date: "<<d<<endl;
    			cout<<"Time: "<<t;
    		}
    	}
    	system("PAUSE");
    	return(0);
    }
    Be easy on me...only 14

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Ok, I compiled this on MS-Notepad, so it's VERY likely that there are errors etc. Anyways, here goes.

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void GotoXY  ( int X, int Y ) 
    { 
        COORD Cursor_Position; 
    
        Cursor_Position.X = X; 
        Cursor_Position.Y = Y; 
    
        SetConsoleCursorPosition( GetStdHandle ( STD_OUTPUT_HANDLE ), Cursor_Position ); 
    }
    
    void FindXY( int &X, int &Y )
    {
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
    	GetConsoleScreenBufferInfo(stdOut, &csbi);
    
    	X = csbi.dwCursorPosition.X;
    	Y = csbi.dwCursorPosition.Y;
    }
    
    int main()
    {
    	char d[9], t[9]; // Whoops, forgot these
    	int x, y;
    
    	for(;;)
    	{
    		if( kbhit() )
    		{
    			cout<< 'X';
    
    			Sleep( 100 );	// So the screen won'e be filled with X
    		}
    		else
    		{
    			FindXY( x, y );
    			GotoXY( 0, 0 );
    
    			cout<< "                     \r"; // May need to add more. I dunno.
    
    			_strdate(d);
    			_strtime(t);
    
    			cout	<< "Date: " << d << ", " 
    				<< "Time: " << t;
    
    			GotoXY( x, y );
    
    		}
    	}
    
    	return(0);
    }
    EDIT: there may be a need for conio for kbhit(), I can't remember.
    Last edited by twomers; 07-06-2006 at 03:59 AM.

  11. #11
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    which library/header file are you using to enter graphics mode ???
    It may have its own function to clear the screen.

  12. #12
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Got 4 errors from your code twomers:
    Originally Posted by twomers

    Ok, I compiled this on MS-Notepad, so it's VERY likely that there are errors etc. Anyways, here goes.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    #include <ctime>
    
    using namespace std;
    
    void GotoXY  ( int X, int Y ) 
    { 
        COORD Cursor_Position; 
    
        Cursor_Position.X = X; 
        Cursor_Position.Y = Y; 
    
        SetConsoleCursorPosition( GetStdHandle ( STD_OUTPUT_HANDLE ), Cursor_Position ); 
    }
    
    void FindXY( int &X, int &Y )
    {
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
    	GetConsoleScreenBufferInfo(GetStdHandle ( STD_OUTPUT_HANDLE ), &csbi);
    
    	X = csbi.dwCursorPosition.X;
    	Y = csbi.dwCursorPosition.Y;
    }
    
    int main()
    {
    	char d[9], t[9]; // Whoops, forgot these
    	int x, y;
    
    	for(;;)
    	{
    		if( kbhit() )
    		{
    			cout<< 'X';
    
    			Sleep( 100 );	// So the screen won'e be filled with X
    		}
    		else
    		{
    			FindXY( x, y );
    			GotoXY( 0, 0 );
    
    			cout<< "                     \r"; // May need to add more. I dunno.
    
    			_strdate(d);
    			_strtime(t);
    
    			cout	<< "Date: " << d << ", " 
    				<< "Time: " << t;
    
    			GotoXY( x, y );
    
    		}
    	}
    
    	return(0);
    }
    EDIT: there may be a need for conio for kbhit(), I can't remember.
    Be easy on me...only 14

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. displaying date and time
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 11:47 AM
  2. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  3. Displaying time and date of files
    By wozza in forum C Programming
    Replies: 3
    Last Post: 05-25-2002, 11:53 AM
  4. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM
  5. displaying the time
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2001, 03:43 AM