Cool c++ program

Feb 24, 2012 at 10:38am
A useful C++ program that allows you to shut down your PC after a period of time.
If you have any suggestions share them.
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
#include<iostream>
#include<time.h>
#include<math.h>
using namespace std;
int main()
{
	int hour=0,min=0,sec=0,avarage;
	int chour,cmin,csec,cavarage;
	
	sec=time(0)%60;
	min=time(0)/60%60;
	hour=time(0)/3600%24-2;
	cout<<"Current time\nHH:MM:SS-"<<hour<<":"<<min<<":"<<sec<<endl;
	cout<<"Enter the time when you want to shutdown HH:MM:SS\n";
	cin>>chour>>cmin>>csec;
	cavarage=csec+cmin*60+chour*3600;
	while(1)
	{
		sec=time(0)%60;
		min=time(0)/60%60;
		hour=time(0)/3600%24-2;
		avarage=sec+min*60+hour*3600;
		avarage=abs(avarage-cavarage);
		system("CLS");
		cout<<avarage/3600%24<<":"<<avarage/60%60<<":"<<avarage%60<<" Remaining!"<<endl;
		if(chour==hour&&cmin==min&&csec==sec) break;
	}
	system("shutdown -r");
}
Feb 24, 2012 at 11:42am
I suggest you to call <time.h> and <math.h> <ctime> and <cmath> respectively. And instead of using system, use Win32.
Feb 24, 2012 at 12:07pm
What's the difference between using system commands and Win32?
Feb 24, 2012 at 12:39pm
With system(something) under windows basically does the same thing as if you had opened a CMD shell and entered your commands there, with Win32 you can call windows API functions directly - how much of a difference that makes in Windows I am not sure, "shutdown" could be the name of a program, but it could also be a command. Either way, windows gives precedence to local files so if you call "shutdown" and you have a file called "shutdown" or "shutdown.exe" in your working directory, windows will attempt to execute that.
Feb 24, 2012 at 12:45pm
for system() look at this: http://www.cplusplus.com/forum/articles/11153/

your program uses 100% of the cpu time which is not that cool... Use sleep() to wait for the amount of time. Wait at least 1 second for you countdown

By the way: you can use strftime() to format your output: http://www.cplusplus.com/reference/clibrary/ctime/strftime/
Feb 24, 2012 at 3:44pm
It says system should have a prototype?!
Topic archived. No new replies allowed.