Countdown Program. Suggest any improvements?

Hey guys!
I am getting my hands dirty in C++. I decided to write a code that counts down. The program then asks the user to input a time. The program takes in the inputted variables and checks them to see if they're valid (ie. hour cannot be less than 0, minute can not be greater than 59 or less than 0, etc). Then the program counts down to 0! The program works using the sleep function and goes all the way to 0. Now, my question is, in your opinion, does it look like an acceptable program to you? If you could change it in any way, what would you do to it, and why? The reason why I am asking is because I want to know if there are different methods to solving this, whether it's by writing shorter code or through using complex programming out there. I am just using whatever basic C++ program I have obtained from a C course I have taken in college.

Also, I am considering building a stopwatch using this similar style. Should I stick with this style or should I start new? Thanks a lot and I hope to hear some comments on how I can improve myself as a programmer!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <iostream>
#include <iomanip> //library to set up leading 0s.
#include <unistd.h>  //library to let me delay

using namespace std;

int time (int time1, int time2, int time3);
unsigned int sleep(unsigned int second); //function to allow me to delay

int main ()
{
    int hour, second, minute;

    
    cout << "Please enter the hour, if there is one:  ";
    cin >> hour;
    cout << "Please enter the minute, if there is one:  ";
    cin >> minute;
    cout << "Please enter the second:  ";
    cin >> second;
    
    if (hour < 0){
        cout << "You have inputted an invalid value for hours. Please restart the program. \n";
    }
        if (minute > 59 || minute < 0){
            cout << "You have inputted an invalid value for minutes. Please restart the program. \n";
    }
            if (second > 59 || second < 0){
        cout << "You have inputted an invalid value for seconds. Please restart the program. \n";
    }
    else
    {
        cout << "You have inputted: " << setfill('0') << setw (2) << hour << ":" << setfill('0') << setw(2) << minute << ":" << setfill('0') << setw(2) << second << endl; //puts up the time you have inputted followed with leading 0s.
        time(second, minute, hour); //inputted files go to function.
    }

    return 0;
}

int time(int second, int minute, int hour) //function that makes the stop watch work. 
{
    int i, j, k;
    i = second;
    j = minute;
    k = hour;
    
    for (i = second; i >= 0; i--) //seconds is under a for loop
    {
        sleep(1); //delay of stopwatch by one second (duh)
        cout << setfill('0') << setw(2) << k << ":" << setfill('0') << setw(2) << j << ":" << setfill('0') << setw(2) << i; //prints out time. first, it prints out the inputted time, goes through the loop, and prints out the approriate values based on the loops function. 
        cout << " \n";
        
        if ((i == 0) && (j >0)){  //if second is a 0 while the minute isn't, a minute is subtracted and the second is set to 60. this then send the values back to the for loop and goes through it again. 
            j--;
            i = 60;
        }
        
        if (( j == 0) && (k > 0)){ //if the minute hits 0 while hour is greater than 0, hour is subtracted by 1, minute goes back to 59, and second goes back to 60, and it goes through the loop again. 
                k--;
            j = 59;
            i = 60;
        }
    }
    sleep(1);
    cout << "DONE!"; //prints done when the loop is over. 
    
    return 1;
}
you can make the calculation way easier if you just deal with seconds:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
time(second + minute * 60 + hour * 60 * 60); //inputted files go to function.
....

int time(int second) //function that makes the stop watch work. 
{
    for (int i = 1; i < second; i++) //seconds is under a for loop
    {
        sleep(1); //delay of stopwatch by one second (duh)
        const int remain_sec = second - i;
        cout << setfill('0') << setw(2) << remain_sec / (60 * 60) << ":" << setfill('0') << setw(2) << remain_sec / 60 << ":" << setfill('0') << setw(2) << remain_sec % 60; //prints out time. first, it prints out the inputted time, goes through the loop, and prints out the approriate values based on the loops function. 
        cout << " \n";
    }
    sleep(1);
    cout << "DONE!"; //prints done when the loop is over. 
    
    return 1;
}
Since you asked I took a look at it.
Nice program, love the comments.
I could not get it to compile because of Line 9. I wonder what program you wrote this with? I tried with DevC++ and Codeblocks.

I found a bug, looks like you handle non numbers ok for hours and mins but not for seconds.


c:\temp>test125
Please enter the hour, if there is one:  1
Please enter the minute, if there is one:  2
Please enter the second:  a

You have inputted: 01:02:00


I made some other changes to fit my style, nothing important, since we are all learning here i'll post so you can see.

I have a similar project, mine is designed to count down and tell you how long a program runs : What I found most interesting is that yours take 2-3 seconds to finish even from zero seconds. To test that I hard coded the time, I also tested this for 5 mins and it added 3 seconds. So it looks like your program takes about 3 seconds to run no matter what. That really isn't a problem, but you might find a way to improve it now that you know about it.

Modification to your code below:
Only major change is I took out your sleep function and used the built in Sleep.
I moved Main() to the end, and relocated a couple of your comments to make it more readable for me.
Then I hard coded in the time so that it would run without user input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iomanip>      //library to set up leading 0s.
#include <unistd.h>     //library to let me delay
#include <iostream>		// for cin, cout and cerr
#include <windows.h>	// for Sleep

using namespace std;	//

int time (int time1, int time2, int time3);
// unsigned int sleep(unsigned int second); //function to allow me to delay

int time(int second, int minute, int hour) //function that makes the stop watch work.
{
    int i, j, k;
    i = second;
    j = minute;
    k = hour;

    for (i = second; i >= 0; i--) //seconds is under a for loop
    {
        Sleep(1000); //delay of stopwatch by one second (duh)
        
//prints out time. first, it prints out the inputted time, goes through the loop, and prints out the approriate values based on the loops function.
		cout << setfill('0') << setw(2) << k << ":" << setfill('0') << setw(2) << j << ":" << setfill('0') << setw(2) << i; 
        cout << " \n";

//if second is a 0 while the minute isn't, a minute is subtracted and the second is set to 60. this then send the values back to the for loop and goes through it again.
        if ((i == 0) && (j >0)){  
            j--;
            i = 60;
        }
//if the minute hits 0 while hour is greater than 0, hour is subtracted by 1, minute goes back to 59, and second goes back to 60, and it goes through the loop again.
        if (( j == 0) && (k > 0)){ 
                k--;
            j = 59;
            i = 60;
        }
    }
    Sleep(1000);
    cout << "DONE!"; //prints done when the loop is over.

    return 1;
}

int main ()
{
	
// Changed the below to hard code the time to test actual run time.
    int hour=0, second=10, minute=0;

/*
    cout << "Please enter the hour, if there is one:  ";
    cin >> hour;
    cout << "Please enter the minute, if there is one:  ";
    cin >> minute;
    cout << "Please enter the second:  ";
    cin >> second;
*/
// Changed the above to hard code the time to test actual run time.

    if (hour < 0){
        cout << "You have inputted an invalid value for hours. Please restart the program. \n";
    }
        if (minute > 59 || minute < 0){
            cout << "You have inputted an invalid value for minutes. Please restart the program. \n";
    }
            if (second > 59 || second < 0){
        cout << "You have inputted an invalid value for seconds. Please restart the program. \n";
    }
    else
    {
        cout << endl << "You have inputted: " << setfill('0') << setw (2) << hour << ":" << setfill('0') << setw(2) << minute << ":" << setfill('0') << setw(2) << second << endl; //puts up the time you have inputted followed with leading 0s.
        time(second, minute, hour); //inputted files go to function.
    }

    return 0;
}


My program to test runtime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <fstream>		// for ifstream
#include <iostream>		// for cin, cout and cerr
#include <string>		// for the string datatype
#include <cstdlib>		// needed for the exit function
#include <sstream>		// 
#include <math.h>		// 
#include <stdio.h>		// 
#include <time.h>		// 
#include <conio.h>		// 
#include <dos.h>		// 
#include <stdlib.h>		// 
#include <time.h>		// 
#include <windows.h>	// 
#include <sys/stat.h>	// 
#include <cstring>		// 
using namespace std;	// 

int RunMe()
{
cout << "|";
Sleep (125);
cout << "\b";
cout << "/";
Sleep (125);
cout << "\b";
cout << "-";
Sleep (125);
cout << "\b";
cout << "\\";
Sleep (125);
cout << "\b";
cout << "|";
Sleep (125);
cout << "\b";
cout << "/";
Sleep (125);
cout << "\b";
cout << "-";
Sleep (125);
cout << "\b";
cout << "\\";
}

int RunMe2()
{
Sleep (60000);
}


void TimeTest()
{
	time_t start,end;
	char szInput [256];
	double dif;
	time (&start); // Start clock
// call program to Test runtime speed below
//	RunMe(); // Run for 1 second, show spinning wheel
//	RunMe2(); // Run for 60 seconds, verify program reports correct time.
	system("filename.exe"); // Run command line program

	time (&end);	// Stop clock
	dif = difftime (end,start);
	printf ("Program finished in %.2lf seconds \n", dif );
}

int main ()
{
	TimeTest();
	return 0;
}
Last edited on
Note that the setfill manipulator is sticky so you could write this:

1
2
3
4
5
6
7
8
9
    cout << setfill('0');

    for (int i = seconds; i >= 0; i--)
    {
        Sleep(oneSecInMilliSecs);

        cout << setw(2) << k << ":" << setw(2) << j << ":" << setw(2) << i << endl;

        // etc 


rather than

1
2
3
4
5
6
7
    for (int i = seconds; i >= 0; i--)
    {
        Sleep(oneSecInMilliSecs);

        cout << setfill('0') << setw(2) << k << ":" << setfill('0') << setw(2) << j << ":" << setfill('0') << setw(2) << i << endl;

        // etc 


or even

1
2
3
4
5
6
7
8
9
10
11
12
13
    char oldFill = cout.fill('0');

    for (int i = seconds; i >= 0; i--)
    {
        Sleep(oneSecInMilliSecs);

        cout << setw(2) << k << ":" << setw(2) << j << ":" << setw(2) << i << endl;

        // etc

    // etc

    cout.fill(oldFill);


(this last version is useful when you want to avoid getting confused about what settings hold where.)

Where I'm using a self-docummenting variable!

const DWORD oneSecInMilliSecs = 1000;

Andy
Last edited on
Topic archived. No new replies allowed.