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--;
    }
}

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--;
    }
}

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

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

>>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.

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. ;)

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.

Edited 5 Years Ago by doug65536: n/a

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

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.

You can use \r to do the countdown on a single line, looks better. Also have to remove the endl

#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 << '\r' << seconds / 60 << ":" << setw(2) << seconds % 60;
        Sleep(1000);
        seconds--;
    }
}