Log in

View Full Version : C++ Countdown Timer Question



Snorkle?
08-26-2008, 05:09 PM
Ok, i know how to create a very simple countdown timer:



#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

void clrscr()
{
system("cls");
}

main()
{
unsigned int timer;

cout << "Timer Set To: ";
cin >> timer;

for (timer>0;timer--;)
{
Sleep(1000);
}
system("PAUSE");
return EXIT_SUCCESS;
}


But my question to you guys is if there is a way to perform other things in my program while waiting for the timer to finish. If possible, could you please explain? thanks in advance.

btw, i joined the site in 2006 and am back... so yay... lol.

Twey
08-26-2008, 07:52 PM
Why have you included <conio.h> and then used DOS-specific commands to clear the terminal? From Googling, it appears that the relevant function for that is _clrscr(). Additionally, your indentation is inconsistent, and you have omitted the argument and return types for main(), which is considered bad style.

Judging from your level of code here, you're not nearly ready for this yet, but here's an introduction to the hell of low-level threaded programming in Windows: http://www.mycplus.com/tutorial.asp?TID=288.

I must say, if you're just starting out with C/C++ programming, you've picked one of the worst operating systems on which to begin. I suggest moving to a POSIX-compliant OS such as Linux or BSD: the interface is much simpler. The Win32 API is a beast, and not something I'd want to tackle at all, to be honest, much less at the same time as trying to learn basic C++.

Snorkle?
08-26-2008, 11:05 PM
Actually i have been doing c++ for about a month taking it seriously.. isnt my first programming language, and i have used linux based OS... i know i left out the the argument and return types for main() that is because i get some funny error when i include <conios.h> and i included that because in the earlier one i was messing with getch() and other pre-defined functions, forgot to take it out.. and the indentation is bad because i didnt take alot of time on messing with this.. infact it took 5 minutes not even.. just wanted to show you an example and see if you could answer the question given above: can i perform other things in my program while waiting for the timer to finish? which i kinda feel like you strayed from....

also im talking about console application not windows application, im definately not ready to mess with WindowsAPI... lol complicated just to make a window pop up for me... so im guessing theres no way?? btw i googled it but got nothing, so maybe it is impossible.

Thanks for the pointers though.. and if you wanna count me as a c++ beginner you most certainly can because im only at using pointers in my book: Sam Teach Yourself C++ In 21 Days... LOL

Twey
08-27-2008, 10:10 PM
can i perform other things in my program while waiting for the timer to finish? which i kinda feel like you strayed from....I did not, although perhaps I did not make my answer quite clear enough: you can indeed, most likely using threads (thus, the tutorial), although you may also be able to set something up using polling.
i know i left out the the argument and return types for main() that is because i get some funny error when i include <conios.h>I'm not sure of the relation here... perhaps fixing that error would be a more appropriate response.
and i included that because in the earlier one i was messing with getch() and other pre-defined functions, forgot to take it out..I did not mean to imply that you should remove it from your program; rather, in fact, that you should make more extensive use of it (in this case, to avoid calling external applications).
also im talking about console application not windows application, im definately not ready to mess with WindowsAPI...The difference is between 'Windows application' and 'application for other OS', not between 'Windows console application' and 'Windows GUI application'. Anything complex in Windows requires fiddling with the Windows API, including non-graphical endeavours such as network or threaded programming.
i have used linux based OS...That's nice. My suggestion was that you should be learning to program with it, rather than starting on Windows. Once you are familiar with the language itself and the paradigm involved, then you can switch to Windows and perhaps begin tackling the Windows API and the horrors contained within.
Thanks for the pointers though.. and if you wanna count me as a c++ beginner you most certainly can because im only at using pointers in my book: Sam Teach Yourself C++ In 21 Days... LOL*laughs* After a month? Shame on you! :)

Snorkle?
08-28-2008, 03:26 PM
*laughs* After a month? Shame on you!

yeah well, i've been skipping a few days and forgetting to read and learn, so yeah. Alright thanks for the help, i'll look into it all.

ps: i can see your posts now, wierd.

magicyte
09-01-2008, 11:04 PM
Actually i have been doing c++ for about a month taking it seriously.. isnt my first programming language, and i have used linux based OS... i know i left out the the argument and return types for main() that is because i get some funny error when i include <conios.h> and i included that because in the earlier one i was messing with getch() and other pre-defined functions, forgot to take it out.. and the indentation is bad because i didnt take alot of time on messing with this.. infact it took 5 minutes not even.. just wanted to show you an example and see if you could answer the question given above: can i perform other things in my program while waiting for the timer to finish? which i kinda feel like you strayed from....

also im talking about console application not windows application, im definately not ready to mess with WindowsAPI... lol complicated just to make a window pop up for me... so im guessing theres no way?? btw i googled it but got nothing, so maybe it is impossible.

Thanks for the pointers though.. and if you wanna count me as a c++ beginner you most certainly can because im only at using pointers in my book: Sam Teach Yourself C++ In 21 Days... LOL

Heh, I use Sams Teach Yourself C++ In 24 hours. I'm not even on Hour 16! You must be better with C++ than I am... Plus, to the guy who used system("cls"), that should be used minorly. It slows the computer down if you use it more than once in a single session.

-magicyte

jaime066
11-14-2008, 12:22 PM
may i know how to make a countdown timer program with design in turbo c. help me plsss.. thxs plss send in my yahoomail. jaime_ortega066@yahoo.com.ph

thxs

magicyte
11-15-2008, 07:51 PM
design in turbo c.

What do you mean by 'design'? Graphics?

-magicyte