1

I just started learning C++ and I need some help.

targetDistance is a float variable and I want to add a string "a" to it, is it possible?

I tried this:

  targetDistance = targetDistance <<"a"

It gives me this error:

 invalid operands of types 'float' and 'const c'
Waqar
  • 8,558
  • 4
  • 35
  • 43
strilz
  • 63
  • 7
  • 4
    No, this is impossible. I wonder what your intention is to "add" a string to a floating point number? Sounds like a misconception to begin with. – lubgr Aug 07 '20 at 11:00
  • Do you want `targetDistance` to become a string after you do that? – HolyBlackCat Aug 07 '20 at 11:04
  • There are some really [good C++ books](https://stackoverflow.com/a/388282/4641116) available to help learn how to program in C++. – Eljay Aug 07 '20 at 11:07
  • 1
    as in like, in python i would do `targetDistance = targetDistance+"a"` – strilz Aug 07 '20 at 11:10
  • @strilz: C++ is different from Python. In Python, assignment to a variable also sets its type. In C++, the type doesn't change, so it has to match. (at least roughly - minor conversions like int to float are allowed) – MSalters Aug 07 '20 at 15:15

2 Answers2

1

If targetDistance is a float, you need to convert it to a string before you can concatenate it with another string. For example:

auto result = std::to_string(targetDistance) + "a";
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Note to OP: `auto` tells the compiler to guess the type. In this case `std::string` is guessed, so it's equivalent to writing `std::string result = ...;`. – HolyBlackCat Aug 07 '20 at 11:06
  • got this error message `'to_string' is not a member of 'std` – strilz Aug 07 '20 at 11:08
  • @HolyBlackCat guess -> infer before the 5 minutes is up? – Bathsheba Aug 07 '20 at 11:09
  • 2
    @strilz • `std::to_string` is declared in ``, and you need to be in C++11 compiling mode or later (I use C++17 compiling mode for my programs). How to specify which C++ standard you are compiling against varies by compiler. – Eljay Aug 07 '20 at 11:10
  • 1
    @HolyBlackCat • guess -> deduce – Eljay Aug 07 '20 at 11:13
  • I am using the c++ software they have with the Arduino – strilz Aug 07 '20 at 11:15
  • @Bathsheba Wanted to use a simpler word at first, and didn't see your comment until 5 mins were up. Oh well. – HolyBlackCat Aug 07 '20 at 11:19
  • @strilz: You need to enable C++11 and `#include ` for the code in my answer to work. For how to enable C++11 with Arduino, see: https://stackoverflow.com/questions/16224746/how-to-use-c11-to-program-the-arduino – John Zwinck Aug 07 '20 at 12:28
0

The idea is to convert the float variable(in this case targetDistance) into a string. Make sure that you have included this header:

#include <string>

The code below:

string s; //to store our float variable
s= to_string( targetDistance ); //to_string function converts into string 
s= s+ "a";

Here is just the short version of it:

string s = to_string( targetDistance ) + "a" ;
Sanzid Sadman
  • 139
  • 3
  • 12