i just started using c++ and ive been using this website alot. its been alot of use. thanks all you guys
I saw a thread today about using the dos prompt to shut windows down. i had a idea to include this in a program.
the command simple enough its
shutdown
then followed by
-s for shutdown
-r for restart
-l for logout
-t for length of time before shutdown/restart/logout
this is what i got so far
int main()
{
int choice, time;
cout<<"Windows Shutdown\n\n";
cout<<" Do you want to\n";
cout<<" 1. Shutdown\n";
cout<<" 2. Restart\n";
cout<<" 3. Log Off\n";
cin>> choice;
cout<<"\n\nIn how long (s)?\n";
cin>> time;
switch( choice )
{
case 1://shutdown
system("shutdown -s");
break;
case 2://restart
system("shutdown -r");
break;
case 3://logoff
system("shutdown -l");
break;
default://wrong entry
cout<<"Error, Invalid input, exiting\n";
break;
}
return 0;
}
i dont know to use a varible to express the time value
eg system("shutdown -s -t " time)
that dont work is there a way ?
am i taking the right approach
thanks in advance
You shouldn't need to shutdown ur computer from a programme. Please tell me why u need 2 b4 I help u?
In any case, I assume u use windows, so u should use window commands for shut down. System commands are frowned upon. :eek:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Try something like this
system("shutdown -s -t 40")
It will shutdown after 40 seconds.
If you want to insert a user specified time, first prepare the command line using sprintf.
char command[ 20 ] = "";
cin >> time;
sprintf( command, "shutdown -s -t %4d", time );
system( command );
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
Using the system commands is not bad always. It is bad when you have a way of doing it through the compiler but you are using the system commands instead. This is because the system command invokes a shell process and waits will it finishes. Since starting a new process takes a lot of overhead the compiler operations are prefered.
For example
using cin.get(); is prefered to make the console wait for a the user to press a key than system("pause" );
But in the case of shutting down, since C++ does not offer a machine shutdown command you will have to use the system() command or use one of the Win32 APIs provided for that. I think it is better to use the Win32 APIs for that since the new process overhead does not apply for it.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
I don't know the statistics.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115