2012-01-20 73 views
2

下面是一个程序,我写了一个运行良好的时候我型我想要的Linux命令它执行Linux系统功能

include iostream 
include string 
using namespace std; 

int main() 
{ 
    cout << "The directory!"; 

    system("cd CS_204"); 

    return 0; 
} 

不过下面我试图使它这样用户就可以在命令中键入他们想要的,我得到他们不能转换std::stringconst char*这是我第一次使用该功能,我拼命试图了解它。帮帮我!!

int main() 
{ 
    cout << "The directory!"; 

    string word; 


    cin >> word 

    if(word != "A") 
     system(word); 

    return 0; 
} 

回答

3

在第二种情况下,word类型的std :: string的和不等于为const char *。你需要使用成员函数std::string::c_str()

system(word.c_str()); // This will convert to a c style string. 
+0

谢谢你们两个了获得C风格的字符串,我会记得把它标记为功课下次对不起!但是,谢谢! – CIM