-3
float diff = 0;
const char* str[] = {"Err: ZPROBE: ",diff};
LCD_ALERTMESSAGEPGM(str);

With the code above I get I get this error. Anyone know how to create a single string from "Err: ZPROBE: " and a (float) diff?

exit status 1
cannot convert 'float' to 'const char*' in initializatio

Sorry should add that in the environment i'm using - 'string' : is not a member of 'std',

Ok now trying this

String str = String("Err: ZPROBE: " , diff);

but get this

call of overloaded 'String(const char [14], float&)' is ambiguous
Lee
  • 381
  • 1
  • 7
  • 16
  • 1
    Use `snprintf`. Or better yet, use `std::string` and pass `c_str()` to the macro. – Hatted Rooster Oct 14 '17 at 16:26
  • 2
    I regret to inform you that it's not just Arduino that can't do it, but every C++ compiler in existence because this is not valid C++. If your intentions are really to write C++ code, then open your C++ book to the chapter that explains how to use `std::ostringstream`, and read it. – Sam Varshavchik Oct 14 '17 at 16:28
  • 1
    For strings in Arduino you should probably use [the Arduino standard `String` class](https://www.arduino.cc/en/Reference/StringObject) (which is *not* the same as the standard C++ `std::string` class). – Some programmer dude Oct 14 '17 at 16:32
  • Sorry should add that in the environment i'm using - 'string' : is not a member of 'std', – Lee Oct 14 '17 at 16:33
  • @RickAstley with String str = String("Err: ZPROBE: " + diff); I get invalid operands of types 'const char [14]' and 'float' to binary 'operator+' – Lee Oct 14 '17 at 16:42
  • 1
    Read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and stop guessing – Passer By Oct 14 '17 at 16:53
  • 2
    I will reiterate that. You will not get very far in C++ by making random guesses how C++ code that needs to do a specific task should look like. C++ is, arguably, the most complicated general purpose programming language in use today. The only way to learn it is in either in a class, with a good instructor, or by reading a C++ book. Trying to randomly put together various bits and pieces of what you think looks like valid C++ code, and then seeing if it works, is a waste of time. – Sam Varshavchik Oct 14 '17 at 17:00
  • I understand where you are coming from but I honestly hardly ever touch c++. This is just a simple hack I need to do to an existing program – Lee Oct 14 '17 at 17:02
  • 1
    @Lee This "quick hack" is essentially someone else writing everything. You are not going to fix it by guessing – Passer By Oct 14 '17 at 17:09
  • It's hardly guessing, I read the docs and try something to the best of my understanding. – Lee Oct 14 '17 at 17:11
  • 1
    When you ask about `String str = String("Err: ZPROBE: " + diff)`, you are guessing. It might be an educated guess, but a guess non the less – Passer By Oct 14 '17 at 17:17
  • From the docs here it looked plausible https://www.arduino.cc/en/Tutorial/StringConstructors many languages are capable of of intelligently realising that a string + float should = string – Lee Oct 14 '17 at 17:18
  • @Lee - You are still just guessing. And `string` is C++, but `String` is not. And guessing still doesn't work. While `string literal`+ `float` doesn't compile, `string literal`+ `int` *does*, but with a result totally different from the one you expect. Please listen to Sam, and read up on this! – Bo Persson Oct 14 '17 at 17:40

1 Answers1

0

To convert float or double into a string, you can use dtostrf() which is available in the stdlib.h header. Also see the reference of String Object Constructors to construct String objects correctly.

Seeker
  • 126
  • 2
  • 8