-1

I have 2 integers, whom I converted to const char* by passing to a user defined function. Now I want to append these 2 variables into a command line string as

    "gnome-terminal -x sh -c 'cd; cd project/into_bot/; sh ./matlab_batcher.sh localize \""+num1+","+num2+"\"; exec bash;

I know its very basic, but am bad with data types. how do I append these 2 const char here? This method is not working as it throws error, saying binary operator for const char*. Please help me though its rudimentary.

num1 and num2 are the variables

Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92
  • 1
    The easy way is C++: http://en.cppreference.com/w/cpp/string/basic_string – chris Jan 26 '13 at 03:14
  • I am using c++! The const char* is not working with the addition. am asking if there's an easier way. pls help me in this case. – Lakshmi Narayanan Jan 26 '13 at 03:16
  • num1 and num2 are char *? or int? – billz Jan 26 '13 at 03:17
  • @LakshmiNarayanan: Yeah, try actually _reading_ what _chris_ said, and following the link he gave you. It seems that you didn't do that, instead just choosing to ask for help again, which was completely redundant. – Lightness Races in Orbit Jan 26 '13 at 03:18
  • 1
    @LakshmiNarayanan, I mean that C++ provides `std::string`, whereas C does not. With just `const char *` everywhere, you'd be limited to the likes of `strcat`, which is less than desirable with the ease of `std::string` available. – chris Jan 26 '13 at 03:18
  • Well, I did. and I ended up seeing the functions I used and got tangled with. I am really sorry, but I was busy with lot other algorithms that suddenly when the last part of the code boils down to this issue, I lost all my patience, and yeah, its past morning 9 here and am yet to hit the bed.! @ Non-Stop Time Travel. I kinda lost my patience here. – Lakshmi Narayanan Jan 26 '13 at 03:20
  • @chris Thanks for the help man. I think I got the problem. Another guy has also explained the same. – Lakshmi Narayanan Jan 26 '13 at 03:23
  • 1
    @LakshmiNarayanan, Well, it's easier to understand my point with an example than a whack of documentation. I think [this page](http://en.cppreference.com/w/cpp/string/basic_string/operator%2B) might have been a better fit to illustrate my point. – chris Jan 26 '13 at 03:25

1 Answers1

2

If num1 and num2 are const char *, you can use std::string.

std::string cmd_line = std::string() +
    "gnome-terminal -x sh -c 'cd; cd project/into_bot/; "
    "sh ./matlab_batcher.sh localize \"" +num1+","+num2+"\"; "
    "exec bash;";

system( cmd_line.c_str() );

Semantically what's happening here is you create a temporary variable with std::string() which is used to build the string, then after everything is built it's used to initialize the permanent variable cmd_line.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421