Skip to content
Advertisement

Concatenate float with a string inside the arguments

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

Advertisement

Answer

<< 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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement