• Advertisement
Sign in to follow this  

C++ Timer (n00b program/mer)

This topic is 4282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, i guess all I want to know is how to make this a little better looking, like how should I not have the seconds left scroll all the way down every time. I'm still learning how to program, this is one of my first programs, so I really have no idea. Any help is appreciated.
#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
int main()
{
time_t seconds;

seconds = time(NULL);
int end;
cout << "How many seconds do you want to wait: ";
cin >> end;
end = seconds + end;

 while (seconds != end)
     {
seconds = time(NULL);
cout <<end - seconds<<"\n";
     } 
     
     
     cout << "\a";
     MessageBox (NULL, "TIME IS UP", ".:Timer by Brad:.", 1);
     
     
   return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
You can do it with win32 API calls.


#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;

void cls();

int main()
{
time_t seconds;

seconds = time(NULL);
int end;
cout << "How many seconds do you want to wait: ";
cin >> end;
end = seconds + end;

while (seconds != end)
{
seconds = time(NULL);
cls(); // clear the screen
cout <<end - seconds<<"\n";
Sleep(3); // added this to make it look better
}


cout << "\a";
MessageBox (NULL, "TIME IS UP", ".:Timer by Brad:.", 1);


return 0;
}

void cls()
{
COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize; /* number of character cells in the current buffer */

/* get the output console handle */
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
/* get the number of character cells in the current buffer */
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
/* fill the entire screen with blanks */
FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);
/* get the current text attribute */
GetConsoleScreenBufferInfo(hConsole, &csbi);
/* now set the buffer's attributes accordingly */
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
/* put the cursor at (0, 0) */
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}



reference

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by dmail
lol or just use system("cls")

Doing that causes the execution of another process, which is slow...
MSDN's way to clear the screen (Win API way) is probably 50x faster then invoking cls.

Share this post


Link to post
Share on other sites
I made some interresting changes after my first post.
Hehe this program is pretty cool. I think Ill go buy me a frozen pizza to try it out


#include <iostream>
#include <ctime>
using namespace std;

#include <windows.h>

void clear_screen();

int main()
{
int end;
time_t seconds, old_seconds; // old_seconds is simply a tracker variable

// Resize console
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rect = { 0, 0, 40, 10 };
SetConsoleWindowInfo(hConsole, TRUE, &rect);

// Set console colors
SetConsoleTextAttribute(hConsole,
FOREGROUND_BLUE | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);

clear_screen(); // Refresh screen

// Ask for user input before we start the timer...
cout << "How many seconds do you want to wait?\n: ";
cin >> end;

// Start timer
old_seconds = seconds = time(NULL);
end = seconds + end;

while (seconds != end)
{
seconds = time(NULL);

// Make sure we only redraw when the remaining seconds has changed
if(seconds != old_seconds)
{
clear_screen();
cout << "\n " << end - seconds;
old_seconds = seconds;
}

Sleep(20); // We dont want to be a CPU time hog !
}

cout << "\a\n\nPIZZA TIME!!!";
//MessageBox (NULL, "TIME IS UP", ".:Timer by Brad:.", 1);

return 0;
}

void clear_screen()
{
COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize; /* number of character cells in the current buffer */

/* get the output console handle */
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
/* get the number of character cells in the current buffer */
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
/* fill the entire screen with blanks */
FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
dwConSize, coordScreen, &cCharsWritten);
/* get the current text attribute */
GetConsoleScreenBufferInfo(hConsole, &csbi);
/* now set the buffer's attributes accordingly */
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen, &cCharsWritten);
/* put the cursor at (0, 0) */
SetConsoleCursorPosition(hConsole, coordScreen);

return;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement