I've found a simple timer program and it seems to work (for the most part). The problem is that certain cout functions aren't showing up. The timer originally didn't include milliseconds in the code, so I added them myself (which could be the problem) based on the code for the other parts of the timer.

The first part that is a problem is that the "Press any key to start" statement doesn't show up. Well, it does briefly, then just begins the program as if it didn't really even exist That wasn't my addition to the code. The second part is that the milliseconds portion of the code doesn't seem to exist, as far as the compiler is concerned. It just doesn't display it. I can't input it once compiled, and it doesn't show up in the program at all.

Any ideas? I'm a beginner so I'm sure I screwed it up somewhere.

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <time.h>
using namespace std;

int main()

{

	 int m,s,h,ms;

	 cout << "A COUNTDOWN TIMER" << endl;
	 cout << "enter time in hours here" << endl;
	 cin >> h;
	 cout << "enter time in minutes here " << endl;
	 cin >> m;
	 cout << "enter time in seconds here" << endl;
	 cin >> s;
	 cout << "enter time in milliseconds here" << endl;
	 cin >> ms;

	 Sleep (50000000);

	 cout << "Press any key to start" << endl;

	 Sleep (50000000);

	 cout << " A COUNTDOWN TIMER" << endl;
	 cout << "time remaining" << endl;
	 cout << "hours: " << h << "mins: " << m << " secs: " << s << " milli: " << ms << endl;

	 for (int hour = h; hour >= 0; hour--)
	 {
		 for (int min = m; min >= 0 ; min--)
		 {
			 if ( min == 0 && h > 0)
				 m = 59;
			for (int sec = s; sec >= 0; sec--)
			{
				if ( sec == 0 )
					s = 59;
			{
			    
				for ( int milli = ms; ms>= 0 ; ms-- );
				if ( ms == 0 ) 
					ms = 99;
				Sleep(50000000);
				system("cls");
				cout << hour << " :hours " << min << " :mins " << sec << " :secs"  << ms << " :milli " << endl;
			}
		 
	 }
	 
	Sleep(50000000);

	 cout << "THE END" << endl;
	 

	return 0;
}

You are using sleep() functions that make the program sleep for 13 hrs 53 min and a few seconds.

What I would suggest is using the clock() function to get the time elapsed since the start of the program in milliseconds.

Here is something that I put together using your code as a base.

#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <time.h>
using namespace std;

#define MS_IN_HR	3600000
#define MS_IN_MIN	60000
#define MS_IN_SEC	1000


int main()
{

	 int m,s,h,ms;
	 time_t totalMS = 0, timeLeft;

	 cout << "A COUNTDOWN TIMER" << endl;
	 cout << "enter time in hours here" << endl;
	 cin >> h;
	 totalMS += h*MS_IN_HR;
	 cout << "enter time in minutes here " << endl;
	 cin >> m;
	 totalMS += m*MS_IN_MIN;
	 cout << "enter time in seconds here" << endl;
	 cin >> s;
	 totalMS += s*MS_IN_SEC;
	 cout << "enter time in milliseconds here" << endl;
	 cin >> ms;
	 totalMS += ms;

	 system("PAUSE"); //people will hate you for using PAUSE but you are already using CLS
	totalMS += clock();

	 cout << " A COUNTDOWN TIMER" << endl;
	 cout << "time remaining" << endl;
	 cout << "hours: " << h << "mins: " << m << " secs: " << s << " milli: " << ms << endl;

	 while(clock() < totalMS)
	 {
	 	timeLeft = totalMS - clock();
	 	system("cls");
		cout << timeLeft/MS_IN_HR << " :hours " << (timeLeft%MS_IN_HR)/MS_IN_MIN << " :mins " << ((timeLeft%MS_IN_HR)%MS_IN_MIN)/MS_IN_SEC << " :secs "  << (((timeLeft%MS_IN_HR)%MS_IN_MIN)%MS_IN_SEC) << " :milli" << endl;
	 }
	 system("cls");
	 cout << "0 :hours 0 :mins 0 :secs 0 :milli" << endl;

	 cout << "THE END" << endl;


	return 0;
}

You might have to include that VC++ header again.

You are using sleep() functions that make the program sleep for 13 hrs 53 min and a few seconds.

What I would suggest is using the clock() function to get the time elapsed since the start of the program in milliseconds.

Here is something that I put together using your code as a base.

#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <time.h>
using namespace std;

#define MS_IN_HR	3600000
#define MS_IN_MIN	60000
#define MS_IN_SEC	1000


int main()
{

	 int m,s,h,ms;
	 time_t totalMS = 0, timeLeft;

	 cout << "A COUNTDOWN TIMER" << endl;
	 cout << "enter time in hours here" << endl;
	 cin >> h;
	 totalMS += h*MS_IN_HR;
	 cout << "enter time in minutes here " << endl;
	 cin >> m;
	 totalMS += m*MS_IN_MIN;
	 cout << "enter time in seconds here" << endl;
	 cin >> s;
	 totalMS += s*MS_IN_SEC;
	 cout << "enter time in milliseconds here" << endl;
	 cin >> ms;
	 totalMS += ms;

	 system("PAUSE"); //people will hate you for using PAUSE but you are already using CLS
	totalMS += clock();

	 cout << " A COUNTDOWN TIMER" << endl;
	 cout << "time remaining" << endl;
	 cout << "hours: " << h << "mins: " << m << " secs: " << s << " milli: " << ms << endl;

	 while(clock() < totalMS)
	 {
	 	timeLeft = totalMS - clock();
	 	system("cls");
		cout << timeLeft/MS_IN_HR << " :hours " << (timeLeft%MS_IN_HR)/MS_IN_MIN << " :mins " << ((timeLeft%MS_IN_HR)%MS_IN_MIN)/MS_IN_SEC << " :secs "  << (((timeLeft%MS_IN_HR)%MS_IN_MIN)%MS_IN_SEC) << " :milli" << endl;
	 }
	 system("cls");
	 cout << "0 :hours 0 :mins 0 :secs 0 :milli" << endl;

	 cout << "THE END" << endl;


	return 0;
}

You might have to include that VC++ header again.

I still habr the same problems. No prompt for a start of the program and no milliseconds input or output.

Nevermind. I fixed it/ am a morom. This works great. Thank you. Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.