Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,247 people online right now. Registration is fast and FREE... Join Now!




Clock in C++ Console?

 
Reply to this topicStart new topic

Clock in C++ Console?

didair
post 30 Aug, 2008 - 02:27 AM
Post #1


New D.I.C Head

*
Joined: 24 Jul, 2008
Posts: 13


My Contributions


Hi!

I wondering if i could do a function in a program that show's the time?

User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 30 Aug, 2008 - 03:05 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


I wrote about this the other day~

cpp
#include <iostream>
#include <ctime>
#include <windows.h>

using namespace std;

// to move back to the beginning and output again
void gotoxy (int x, int y)
{
COORD coord; // coordinates
coord.X = x; coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
}

int main ()
{
time_t now;
while (true)
{
gotoxy (0,0);
now = time(0);
cout << "The time is now: " << ctime(&now);
Sleep (20);
}
cin.get ();
return EXIT_SUCCESS;
}


I use gotoxy to move to the beginning of the console output and overwrite the current time.

Hope this helps smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 07:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month