954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Countdown timer

0
By ben1996123 on Sep 30th, 2011 10:46 pm

Input the minutes, then the seconds, and it will do the rest. Pretty simple but I just thought I'd post it. Made as a quick test for my game.

#include <iostream>
#include <windows.h>
#include <cstdlib>
using namespace std;
int main(){
    int minutes;
    int seconds;
    cin >> minutes >> seconds;
    seconds = (minutes*60)+seconds;
    cout << endl;
    while(seconds>0){
        if(seconds%60<10){
            cout << seconds/60 << ":0" << seconds%60 << endl;
        }
        else{
            cout << seconds/60 << ":" << seconds%60 << endl;
        }
        Sleep(1000);
        seconds--;
    }
}

But it will break if I enter "a" instead of a number... :(

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

You could use setw and setfill functions to avoid that if-statement in your loop. As so:

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

using namespace std;
int main(){
    int minutes;
    int seconds;
    cin >> minutes >> seconds;
    seconds = (minutes*60)+seconds;
    cout << setfill('0') << endl;
    while(seconds>0){
        cout << seconds / 60 << ":" << setw(2) << seconds % 60 << endl;
        Sleep(1000);
        seconds--;
    }
}
mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 
But it will break if I enter "a" instead of a number... :(

Well... why would you want to enter "a"?

ben1996123
Junior Poster in Training
82 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

>>Well... why would you want to enter "a"?

That's called "fool-proofing a program". Generally, when writing a program that deals with a user (either through console input, command-line input, config-file input, or a GUI), you must assume that the user has an IQ of 0, i.e., that he/she is an absolute imbecile that could be inputting anything and click on all or any buttons. That's often what beta-testing is about, have a user do all sorts of random crap and watch if the program can cope with it.

mike_2000_17
Posting Virtuoso
Moderator
2,134 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 
That's called "fool-proofing a program". Generally, when writing a program that deals with a user (either through console input, command-line input, config-file input, or a GUI), you must assume that the user has an IQ of 0, i.e., that he/she is an absolute imbecile that could be inputting anything and click on all or any buttons. That's often what beta-testing is about, have a user do all sorts of random crap and watch if the program can cope with it.


Hahaha, +1 because it is to the point. ;)

Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
 

You can't rely on Sleep to reliably sleep for one second. If you read the documentation for sleep, it will specifically say that it might (and in reality, often will) sleep for more than the specified time period. If the cpu is under load (like if there are processing intensive processes contending for processor time) Sleep will be very inaccurate.

doug65536
Light Poster
35 posts since Sep 2011
Reputation Points: 28
Solved Threads: 3
 

Use QueryPerformanceCounter

LevyDee
Posting Whiz in Training
260 posts since Mar 2010
Reputation Points: 13
Solved Threads: 25
 

very commendable. kudos

ratatat
Newbie Poster
10 posts since Oct 2011
Reputation Points: 10
Solved Threads: 1
 

You may want to prompt user to enter the numbers, they might get scared looking at the blank screen.

limaulime
Newbie Poster
7 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
 

I just realized I should clarify my earlier comments about sleep. I did not mean to imply that you shouldn't call sleep, what I failed to say was, use sleep, but use another way to get how much time actually elapsed, and adapt.

For example: At the start of the loop, get the current "time" (QueryPerformanceCounter is a good way to get it, as LevyDee mentioned). Calculate what the "time" value would be at the end of the delay. Each loop, after the sleep call, check if the next sleep should be less than a second (because the time is almost up) and use that. If you need to wait more than a second more, use a second.

doug65536
Light Poster
35 posts since Sep 2011
Reputation Points: 28
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You