CIM
CIM

Reputation: 41

linux system function

Below is a program i have written that ran fine when I type what linux command I wanted it to perform

include iostream
include string 
using namespace std;

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

    system("cd CS_204");

    return 0;
}

However below I tried to make it so a user can type in the command that they wanted and I get they can not convert std::string to const char* This is my first time using the function and I am desperately trying to understand it. Help!!

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

    string word;


    cin >> word

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

    return 0;
}

Upvotes: 2

Views: 695

Answers (1)

Mahesh
Mahesh

Reputation: 34625

In the second case, word is of type std::string and is not equivalent to const char* . You need to get the c-style string using the member function std::string::c_str()

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

Upvotes: 3

Related Questions