#include <stdlib.h>
#include <stdio.h>
#include<conio.h>

void main()
{
int ch;
clrscr();
printf("\n\n~~~~~~~~~~~~~~~~~SHUTDOWN MENU~~~~~~~~~~~~~~~~~~~\n");
printf("1.Shutdown\n2.Restart\n3.Logoff\n4.Hibernate\n5.exit");
printf("\nEnter choice : ");
scanf("%d",&ch);
switch(ch)
{
  case 1:system("shutdown -s");
         break;
  case 2:system("shutdown -r");
         break;
  case 3:system("shutdown -l");
         break;
  case 4:system("shutdown -h");
         break;
  case 5:exit(1);
         break;
  default:printf("Invalid choice");
}
getch();
}

And your question about this poor code is... what?

You want to show how to shutdown ??????????????????????

And your question about this poor code is... what?

hi...why is the code poor? i mean, i didn't even know we could shutdown the pc using c.
can u please tell me a (better) way? Thanks

@Jason

conio.h it is not part of the C standard library,

clrscr also not standard function ..

And he not doing any magic, just giving his work to system..

It's poor because of

#include<conio.h>
void main()
clrscr();
getch();

And the formatting is only fair.

Second what WaltP says, plus don't use scanf for interactive user input, check the return values of standard library functions, and use fflush(stdout) after printing a prompt that doesn't end with '\n' to make sure it gets printed.

1 is not a portable value to pass to exit(), and returning non-zero usually means that some error occurred. Selecting the "exit" function from the menu shouldn't result in an error.

Finally, I can't imagine what version of "shutdown" you're using if -s means "halt", -l means "log off", and -h means "hibernate".

when I run the code and press y it says access is denied. So what can I do?

when I run the code and press y it says access is denied. So what can I do?

Log in with an account that has permission to shut the computer down and then run the program.

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.