Arduino cannot convert 'float' to 'const char*' in initialization - c++

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

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.

Related

invalid operands of types 'float' and 'const c'

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'
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";
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" ;

C++ invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

WARNING: Extremely limited knowledge of C++ and coding in general. Please refrain from advanced terminology.
for ( i = 0; i < answer.size(); ++i) {
if (guess == answer.at(i)) { //to display correct letters in answerDisplay
answerDisplay.replace( (2 * i), 1, answer.at(i) );
correctGuesses += 1;
}
Given: answerDisplay and answer are strings.
When I run my program there is a compile-time error at the third line of what I've posted saying:
invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
What's the problem? How can I fix it? All other posts with this error talked about pointer characters but I don't know what those are.
Pointer characters are they way plain strings are implemented in C and C++. In C++ you have the nice class std::string, but string literals are still array of characters. And arrays in C and C++ can be seen as pointers.
For example, "hello" is of type const char[6] (5 characters plus the ending NUL), but it can be trivially converted to const char *, and that in turn can be converted to std::string.
In line 3, the only relevant code is a call to the member function std::string::replace(). There are a lot of overrides of this function (different sets of parameters to be used), but the one the compiler is trying to use is this one:
string& replace (size_t pos, size_t len, const char* s);
As you can see, it takes two numbers and a const char * (an old-string/char-array). But you are passing as third parameter answer.at(i) that is of type char. Hence the error:
invalid conversion from ‘char’ to ‘const char*’
Solution? You can build a string from that char:
answerDisplay.replace( (2 * i), 1, std::string(1, answer.at(i))
Or you can get a substring of the original string instead of a plain character.
answerDisplay.replace( (2 * i), 1, answer.substr(i, 1))

Error in printing all substrings of a string

This is in reference to the following answer by Synxis.
https://codereview.stackexchange.com/questions/18684/find-all-substrings-interview-query-in-c/18715#18715
Suppose, I have to print all substrings of the string "cbaa". To do this, I have to invoke the method like this:
findAllSubstrings2("cbaa");
If I take a string from user, and do the following:
string s;
cin>>s;
findAllSubstrings2(s);
it gives the following error:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'void findAllSubstrings2(const char*)'
Why does this happen?
As the error message says the parameter of function findAllSubstrings2 is declared as having type const char * while you are trying to pass an argument of type std::string
string s;
//...
findAllSubstrings2(s);
You should use member function c_str or data (starting from C++ 11) of class std::string. For example
findAllSubstrings2(s.c_str());
you using string, in function is char try to use char[] s;
use c_str() method in string class when passing the argument
string s;
cin>>s;
findAllSubstrings2(s.c_str());
You probably should change the type of the parameter of the function. Somethink like:
void findAllSubstrings2(string s){
//... function implementation...
}

adding a value in to the http request from a long to char

I am trying to to add value in to the http request and getting errors when I add a long into the path.
long test1, test2;
unsigned long age;
numdata=inet.httpGET("test.com", 80, '/system/get.php?value1='+test1+'&value2='+test2, msg, 50);
error: invalid conversion from 'long int' to 'const char*'
And I have tried the following and getting an error.
const char getRequest = '/system/get.php?value1='+test1+'&value2='+test2;
numdata=inet.httpGET("test.com", 80, getRequest, msg, 50);
And am getting the following error
error: invalid conversion from 'char' to 'const char*'
If would be better to use a ostringstream for this
#include <sstream>
std::ostringstream ss;
ss << "/system/get.php?value1=" << test1 << "&value2=" << test2;
then you can get at the std::string from the string stream using
ss.str();
Whatever you choose you should use " instead of single quotes when dealing with a char array. Use only single quotes when dealing with a single char variable.
What you are currently doing here
const char getRequest = '/system/get.php?value1='+test1+'&value2='+test2;
is declaring a const char - that is a single constant character. This is not the same as an array of char.

How to add strings to a 2d array of char elements?

I have the below program written in C++:
#include <iostream>
using namespace std;
int main()
{
int age[5];
char name[5][10];
age[0]=10;
age[1]=20;
age[2]=30;
age[3]=25;
age[4]=40;
name[0]="abc";
name[1]="abc";
name[2]="abc";
name[3]="abc";
name[4]="abc";
cout<<name[0]<<" is "<<age[0]<<"years old";
cout<<"\n";
cout<<name[1]<<" is "<<age[1]<<"years old";
cout<<"\n";
cout<<name[2]<<" is "<<age[2]<<"years old";
cout<<"\n";
cout<<name[3]<<" is "<<age[3]<<"years old";
cout<<"\n";
cout<<name[4]<<" is "<<age[4]<<"years old";
cout<<"\n\n";
system("PAUSE");
}
When I compile and run it, I get these errors:
error C2440: '=' : cannot convert
from 'const char [3]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
I am running MSVC 2008 under Windows 7. I have tried many possible solutions but I failed in fixing this. Any help would be appreciated,
You are treating the name array as if it was defined thus:
char *name[5];
So either define it that way, or use the following code to populate it:
strcpy(name[0], "abc");
strcpy(name[1], "abc");
strcpy(name[2], "abc");
strcpy(name[3], "abc");
strcpy(name[4], "abc");
I prefer the former choice. The point being you are trying to assign a char * to a char [] which is what strcpy is for. Given you are manipulating initialized C strings in this case anyway, you might as well deal with char * throughout the code.
You should use std::string for this purpose. The use of char* and char[] to represent strings is deprecated in C++ for many good reasons.
Given the program snippet, name can be initialized at the declaration itself.
char name[5][10] = { "abc", "abc", "abc", "abc", "abc" } ;
// ^ index 5 is not necessary. char name[][10] = { .. } would also suffice.
Specified the length of each row is 10 but only using first 3 indexes of it. Every 3rd index ( i.e., 4th element in the array ) is automatically added with a '\0'.
Initialization can be done in case of age array too.
You can use also std::string name[10] instead of 2d char's array. In this case only you can assign new values to the strings through operator '='.
Otherwise you should to use array of char* and use strcpy() function for assignment.