0

Right off the bat, no this is not a duplicate. I have been searching for hours to do this but it might be simple as I started coding a few weeks ago.

How do I concatenate a string with a float inside the arguments?

I want to be able to do this, but it is not working:

system("xdotool mousemove " << $2 << " " << $3);

Yes, this is C++, just that I am using this inside of Bison, so the $2 and $3 are variables (which are floats). As in the Bison file I have a lot of things going on, I would appreciate having a solution in one line, like the format I have going on here.

EDIT

The problem here is actually with the concatenation. I did this, but it still doesn't work, and gave me the same error:

string temp = "xdotool mousemove " << $3 << " " << $4; system(temp.c_str());

EDIT 2

Sorry for so many edits, but I tried doing

system(("xdotool mousemove " + to_string($3) + " " + to_string($4)).c_str());

but it still doesn't work :( Any other ideas? This time the error says ‘to_string’ was not declared in this scope

Kind Regards,
Matthew Sanetra

  • What error you get? –  May 06 '17 at 13:14
  • @Lazar I get: `xdoscript.y:28:53: error: invalid operands of types ‘const char [19]’ and ‘float’ to binary ‘operator<<’` –  May 06 '17 at 13:16
  • 1
    Quite close, contains the solution: https://stackoverflow.com/questions/22440067/concatenating-integers-to-const-char-strings – Baum mit Augen May 06 '17 at 13:16
  • I think that you need to read more about what type of argument system() function expect and what you are passing to it. Read more here https://www.tutorialspoint.com/c_standard_library/c_function_system.htm –  May 06 '17 at 13:17
  • @Lazar Doesn't it take a string to do the terminal command? I am just trying to put a variable inside of the string to pass to the terminal... –  May 06 '17 at 13:20
  • system() function does not understand this argument **"xdotool mousemove " << $2 << " " << $3**, look for that –  May 06 '17 at 13:21
  • Btw, `$2` and `$3` are not exactly great variable names. – Baum mit Augen May 06 '17 at 13:23
  • @BaummitAugen I am using a program called Bison, please, research about it, this is the way I reference different parameters. –  May 06 '17 at 13:25
  • @Lazar But it should understand a string. I am only concatenating floats to a string. –  May 06 '17 at 13:26
  • http://stackoverflow.com/a/22140370/6385115 look here –  May 06 '17 at 13:28
  • @Lazar edited. please check the post now. –  May 06 '17 at 13:41
  • 1
    `std::string` does not support concatenation using operator`<<` - that would be `std::stringstream` instead - or you can use the solution described in the link Baum mit Augen provided – UnholySheep May 06 '17 at 13:42
  • @UnholySheep I edited it for Baum mit Augen's comment, but it still doesn't work. Check out the edit. –  May 06 '17 at 14:00

1 Answers1

2

<< is not a string concatenation operator. The usage you are thinking of is:

std::cout << "The value is " << x << '\n';

Careful examination of the above line will show that it does not involve the use of astd::string at all; the left-hand argument of the<< operator is always a std::ostream&. (The first one is explicitly specified and the other two are the result of the previous << operator, which in this case returns astd::ostream&&.)

The error message you are getting from the compiler is precisely correct and you should take the time to understand what it is telling you, at least if your goal is to learn how to write C++ programs.

The C++ standard library overloads the + operator to do concatenation of std::string objects. But a string literal like "xdotool" is not a std::string; it is an array of char. You can also use + to concatenate a std::string with a C-style string (i.e. a NUL-terminated character array), but there must be a std::string as at least one if the arguments.

And there is no automatic conversion from a number to a string, nor is there an overload of + which takes a std::string and a number. If you are using C++11 or more recent, then you could use std::to_string to explicitly convert.

The usual way of using the << operator as you are trying to do involves the use of a output stream-like object which simply creates an in-memory string buffer: a std::ostringstream. Thar object implements the str method to extract the buffer as a std::string; see the example here for something similar to what you want to do.

But, as I mentioned, a std::string is not a simple array of char, so you cannot pass one to a function expecting a C-style string. That includes all functions in the standard C library, like system, which know nothing of C++ std::string. So you need to use the c_str member function of the std::string object to extract its data as a C-style string.

rici
  • 234,347
  • 28
  • 237
  • 341
  • So would this be correct? `ostringstream temp; temp << "xdotool mousemove " << $2 << " " << $3; system(temp.str().c_str());` –  May 06 '17 at 14:22
  • well, no. sorry for the late reply though. –  May 06 '17 at 15:30
  • @matthew: what error did you get? Did you `#include `? – rici May 06 '17 at 16:33
  • omg thank you! it works! (just a tip please always include an include in a post as I am a noob) –  May 06 '17 at 19:58
  • @matthew: The #include was present in the example code I pointed you to :) And a tip in return: Include the text of error messages instead of just saying "it doesn't work" to make life easier for those of us who cannot see your computer terminal. – rici May 06 '17 at 20:16
  • Cheers! I appreciate people like you. –  May 06 '17 at 20:39