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

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:

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

You shouldn't need to shutdown ur computer from a programme. Please tell me why u need 2 b4 I help u?

i just think it would be handy to and to see if there is away to do it

and why is it bad to use system commands?

Thanks wolfpack
it works great now

i never thought of doing it that way

just in the log off option the time doesnt work but it doesnt work either in the command prompt so that ok then

Thanks wolfpack
it works great now

i never thought of doing it that way

just in the log off option the time doesnt work but it doesnt work either in the command prompt so that ok then

also what are the windows commands to shutdown the computer and where could i find a list of them

Its ok i googled it and got a big list and shutdown was on it so shutdown is a windows command

i just used system() to proform it

if im not ment to do it that way is there another way to proform windows commands???

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.

ow k ive been using system("pause") for most of my programs

does it make much of a differece???

This article has been dead for over six months. Start a new discussion instead.