0

I want to execute linux commands using c++ platform and I am using system() but when i write:

cout<<"Enter the command"<<endl;
cin>>arraystring[size];

system(arraystring.c_str()); 

it gives error!

a4.cpp: In function ‘int main()’:
a4.cpp:75:20: error: request for member ‘c_str’ in ‘arraystring’, which is of non-class type ‘std::string [100] {aka std::basic_string<char> [100]}’
 system(arraystring.c_str());

What should i do to overcome this error?

9
  • 2
    How is arraystring defined? Commented Mar 22, 2015 at 6:18
  • char arraystring[100]; Commented Mar 22, 2015 at 6:19
  • is there any other way to execute linux commands using c++ platform?? Commented Mar 22, 2015 at 6:19
  • 2
    Not according to your error message, unless you've used a preprocessor macro to define char as std::string. Commented Mar 22, 2015 at 6:21
  • 1
    char* is not a class it's a pointer to a basic data type in c++, consequently it has no member functions such as c_str() Commented Mar 22, 2015 at 6:50

2 Answers 2

2

Actually, you have defined your arraystring variable as:

string arraystring[100];

Hence, when you write the statement

system(arraystring.c_str);

arraystring being an array of std::string, gives the error.

Change it to:

system(arraystring[size].c_str());
Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

std::string command;
std::cout << "Enter the command" << endl;
std::cin >> command;

system(command.c_str()); 

That will work as long as command is just one word. If you want to use multi-word commands, you can use:

std::getline(std::cin, command);
system(command.c_str()); 

5 Comments

ok thanks it works but is there any other way to run linux commands on c++ platform as we are prohibited to use system call by our instructor.
and i cannnor upvote you need 15 reputations, can you give me a reputation??
@mahnoorfatima, it's ok. You can upvote when you can. There is no need to hurry.
if you have a book in standard c++ programming you wont get so much knowledge about linux system calls but while you are reading standard c++, you can have a look to manual pages of linux c and c++ system calls
in terminal it is very useful to use man page you can try man fork and etc. In your linux terminal comand line you will get a very useful instruction on how to use these functions in your program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.