2

I am trying to include the sleep 1 command in bash in my .cpp file
While system("sleep 1") works fine, I would like to change 1 into a const int or string

const string t = "1";
string c = "sleep " + t;
system(c);

However, it seems like the system(c) is treated as a call to function as the following error occurs:

error: no matching function for call to 'system'
  system(c);

How can I resolve this?

appleline
  • 27
  • 4

2 Answers2

4

The system function takes a const char* pointer as its argument. However, there is no implicit conversion from a std::string object to a const char* (representing its contained string data) provided by the Standard Library. Instead, you can call the c_str() member function on that object, like so:

system(c.c_str());
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
3

system(c.c_str()) or, equivalently (from C++11), system(c.data())

user2052436
  • 4,321
  • 1
  • 25
  • 46