Controlling the computer with C++

Hi everyone. I want to write a code that I can turn off the computer writing to the screen "close". I wrote a code then I can turn my computer off with C++ but via numbers not via strings. Anybody help ? Can anybody write a example code ? Thank you...
Last edited on
cin.getline()
Thanks for your comment but I want a code like when I write "close" comp. will be close also when I write "restart" comp will be restarted. How can I do that ? Code is like that but I want to use strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include<conio.h>
using namespace std;

int main(int argc, char *argv[])
{
    system("color B2");
    cout<<"****** Enter a number to do shown below ******"<<endl;
    cout<<"1= shut down all programs running now"<<endl;
    cout<<"2= Restart the computer"<<endl;
    cout<<"3= Shot Down the computer"<<endl;
    int x;
    cin>>x;
 for(;;){   
    if(x==1){ system("shutdown -f"); }
    if(x==2){ system("shutdown -r"); }      
    if(x==3){ system("shutdown -s"); }
    break;
    
    else {  cout<<"enter again"; }
}
 
    system("pause");
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
#include <iostream>
#include <cstdlib>
#include <cstring>

int main()
{
  string command;
  bool valid = false;
  cout << "Enter a command: ";
  cin >> command;

  do {
  if(strcmp(command, "restart") == 0) {
    system("shutdown -s");
    valid = true;
  }
  else if(strcmp(command, "shutdown") == 0) {
           system("shutdown -r");
           valid = true;
  }
  else if(strcmp(command, "kill") == 0) {
           system("shutdown -f");
           valid = true;
  }
  }while(!valid);
}


Somthing like this ;)

BTW, Excuse me but your code is very bad.
Last edited on
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
30
31
32
33
34
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int main() {
    system("color B2");
    bool valid=false;
    string command;
    for(;;){
    cout<<"****** write sth ******"<<endl;
   
    cin>>command;
    do {
    if(strcmp(command, "restart")==0) {
    system("shutdown -s");
    valid=true; }
    
    else if (strcmp(command, "shuntdown")==0) {
    system("shuntdown -r"); 
    valid true; }
    
    else if (strcmp (command,"kill")==0) {
    system("shutdown -f");
    valid =true; }
  
    
    else
     cout<<"enter again";
}
system("pause");
return 0;
}


I write a code like that and it does not run. compiler error : no matching functio for call to 'strcmp(std::string&,const char[8])

What is the problem here ?
Last edited on
excuse me, you should write your code like this.
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
30
31
32
33
34
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int main() {
    system("color B2");
    bool valid=false;
    string command;
    for(;;){
    cout<<"****** write sth ******"<<endl;
   
    cin>>command;
    do {
    if(strcmp(command.c_str(), "restart")==0) {
    system("shutdown -s");
    valid=true; }
    
    else if (strcmp(command.c_str(), "shuntdown")==0) {
    system("shuntdown -r"); 
    valid true; }
    
    else if (strcmp (command.c_str(),"kill")==0) {
    system("shutdown -f");
    valid =true; }
  
    
    else
     cout<<"enter again";
}
system("pause");
return 0;
}


use c_str() function of string class, It returns a const char*
Last edited on
Thanks for your help but your code still does not work. I Think there is missing small things.
BTW I wrote a code which can make same things likely:

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
30
31
using namespace std;

int main() {
    system("color B2");
    char command[100];
    int final;
    for(;;){
    cout<<"****** write sth ******"<<endl;
   
    gets(command);
    final=strcmp(command, "restart");
    if(final==0) {
    system("shutdown -r");
    break; }
    final=strcmp(command, "shutdown");
    if (final==0){
    system("shuntdown -s");
    break; }
    final= strcmp (command,"kill");   
    if (final==0){
    system("shutdown -f");
    break; }
    final=strcmp (command,"ULAS");
    if (final==0) {
    system("shutdown -s -t 1");
    break; }
   
}
system("pause");
return 0;
}


Finally it works :)
Topic archived. No new replies allowed.