Oct 2, 2012 at 4:44pm UTC
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 Oct 2, 2012 at 5:09pm UTC
Oct 2, 2012 at 5:43pm UTC
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 Oct 2, 2012 at 7:02pm UTC
Oct 2, 2012 at 7:00pm UTC
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 Oct 2, 2012 at 7:08pm UTC
Oct 3, 2012 at 4:16am UTC
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 Oct 3, 2012 at 4:16am UTC