• C++, how the fuck do I make a countdown timer?
    17 replies, posted
I was bored today, so I decided to put my very basic C++ skills to the test. I made a very simple and pointless little math-solving-game-thing. I have two rand()'s, r1, and r2. At the beginning of the program, you're asked whether you want to work with addition, or subtraction. Pick one, and the program will challenge you with a question. For example: What is 3450-2130? Answer correctly, and you'll earn a point, answer incorrectly, and you'll lose a life. After you've lost all five lives, a text will appear telling you that you've lost, and that you'll exit the program by pressing any key of your choice. Now, while this works fairly well, there's just not enough challenge to it. You having a certain period of time on you to solve the problem, would add atleast some challenge to it, though. And this is where I'm stuck. At first I thought about adding something simple such as do{ Sleep(1000) time-- }while (time>0); And then simply have another do/while loop cout:ing the time. Ofcourse this won't work, though. Seeing as the beginning of my code looks like this: (Terribly sorry about it being in Swedish, but it should be clear enough). [code] int r1 = rand(); int r2 = rand(); int rsvar=r1+r2; cout << "Vad blir" << endl << r1 << "+" << r2 << endl <<endl ; cout << "Poang:" << poang << endl; cout << "Liv:" << liv << endl << endl; cin >> svar; [/code] If I put the do/while loop before I enter the answer(svar), I'll have to wait 30 seconds until I'm able to enter anything, and if I put it after, I'll have to wait 30 seconds before I get the next question. So if I've been unclear: I want a timer to count from 30 to 0. I also want you to be able to see it counting down, in real time, while you solve the problem. If the counter reaches zero, the program will subtract your life with five, instantly ending the game, via a simple if (time==0) {life-5; } phrase. How would I do this, though? Help is appreciated. Oh, and the whole code, also with Swedish words: [code] #include <iostream> #include <cstdlib> using namespace std; int main() { int poang=0; int liv=5; int val; int svar; int time=30; cout << "Vilket raknesatt vill du arbeta med?" << endl << endl << endl; cout << "1. Addition" << endl << endl; cout << "2. Subtraktion" << endl << endl; cin >> val; srand(time(0)); if (val==1) { //Additionsdel do{ int r1 = rand(); int r2 = rand(); int rsvar=r1+r2; cout << "Vad blir" << endl << r1 << "+" << r2 << endl <<endl ; cout << "Poang:" << poang << endl; cout << "Liv:" << liv << endl << endl; cin >> svar; if (svar==rsvar) { cout << "Ratt!" <<endl <<endl ; poang++; } else { cout << "Fel!" <<endl << endl; liv--; } if (liv==0) { cout << "Du har forlorat." << endl << endl; } }while (liv>0); } //Subtraktionsdel else { do{ int r1 = rand(); int r2 = rand(); int rsvar=r1-r2; cout << "Vad blir" << endl << r1 << "-" << r2 << endl <<endl ; cin >> svar; if (svar==rsvar) { cout << "Ratt!" <<endl <<endl ; poang++; cout << "Poang:" << poang << endl; cout << "Liv:" << liv << endl << endl; } else { cout << "Fel!" <<endl << endl; liv--; cout << "Poang:" << poang << endl; cout << "Liv:" << liv << endl <<endl ; } if (liv==0) { cout << "Du har forlorat." << endl << endl; } }while (liv>0); } system("pause"); return 0; } [/code]
You can't do anything while a call to std::cin is waiting for user input, unless you are having a second thread that does your count down.
So how would I go around making use of another thread?
An easy solution would be to download and use SFML's [url=http://sfml-dev.org/documentation/1.6/classsf_1_1Thread.php] Thread [/url] class. A more complex solution would be to actually implement your own thread functionality as seen as an example [url=http://www.cplusplus.com/forum/windows/3301/] here [/url]
If you're using an up-to-date C++ compiler I would suggest enabling C++0x/C++11 features (with g++ that would be -std=c++0x or -std=c++11) and using the standardized std::thread class. Else I would suggest using [url=http://www.boost.org/]Boost[/url].[url=www.boost.org/doc/html/thread.html]Thread[/url] which is very much alike the C++11 thread.
Thank you for your replies. Also, I'm using Dev-C++ 5. (It was made in 2005 :v:) [editline]3rd December 2011[/editline] Is it generally pain-free to manually install a distribution?
Which is not a compiler (but afair comes with MinGW, so I suppose you are using a very old version of that, which does not support the new standard).
im using MSVT 2003, [img]http://i.somethingawful.com/forumsystem/emoticons/emot-chord.gif[/img]
Assuming your MinGW version includes a copy of process.h, include that and use _beginthread/_beginthreadex. It's not exactly the most portable thing in the world, but it should get the job done.
Well, I do have Microsoft Visual C++ 2010 installed, aswell. I just found Dev-C++ to be the most comfortable. Maybe I should switch over to Visual? [editline]3rd December 2011[/editline] [QUOTE=MallocNull;33553093]Assuming your MinGW version includes a copy of process.h, include that and use _beginthread/_beginthreadex. It's not exactly the most portable thing in the world, but it should get the job done.[/QUOTE] Ah, well, I included <process.h>, and it seems to work. (As in, it doesn't throw an error message at me, which it does if I include for example <coffee>). So what now? Do I just add _beginthread and do the do/while loop there? And [I]where[/I] should I add the code for the thread? As in, how do I tell it "This should run via this thread, the rest of the code by another" ?
[QUOTE=RixxzIV;33553117]Maybe I should switch over to Visual?[/QUOTE] Of course you should, holy... Or, if you want to stick with MinGW, switch over to Code::Blocks, which is as well free. [editline]3rd December 2011[/editline] [QUOTE=RixxzIV;33553117]And [I]where[/I] should I add the code for the thread? As in, how do I tell it "This should run via this thread, the rest of the code by another" ?[/QUOTE] Generally, _beginthread() or the like take another function's address as an argument. This function will then run in the new thread. Check the documentation for the method you use.
Thanks for your help. As I've said, I'm a beginner, so I'll need some time looking through the documentations in order to actually learn it. But you've been a great help.
Honestly though, I think you should stick with the basics for a while and get a good understanding of the language and the code practices in general before doing such "advanced" things like multi-threading, as it yields a whole new set of complexities and problems to take care of.
Probably a good idea.
[QUOTE=RixxzIV;33555262]Probably a good idea.[/QUOTE] If you simply want it to count down from 30 just use a FOR loop. Example: for (int i = 31; i > 0;i--) { //Everything here std::cout<<i<<' '; //prints out the counter } This loop will go from 30-0. Lycka till:)
How does printing 30 integers help you keep track of time A rough, unsophisticated way of doing it would be to use a 1000ms interrupt between iterations but it's hardly ideal. Edit Although this approach obviously makes it impossible to do anything else while the system waits..
Guys, OP's problem is not counting down from 30 to 0, it is doing it while standard I/O is waiting for user input.
1. #include time.h 2. use threads 3. ??? 4. profit?
Sorry, you need to Log In to post a reply to this thread.