Pages: [1]   Go Down
Author Topic: Casting Confusion  (Read 230 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have some variables that I'm printing to an LCD, and also want to tweet.

I'm getting a simple float from a BMP085 using the adafruit library.
lcd.print() takes the float just fine.

I also want to construct a string to send out a tweet of the data.
I'm using http://arduino-tweet.appspot.com/ and it works great for a simple hard-coded const char.

Code:
pres = pres / 1000; //convert to mb from the default
lcd.print(pres, 1); //works great
lcd.print(" mb"); //works great
 
char pres_c = char(pres);
 
char twt = "This is my test tweet. The air pressure is" + pres_c + " mb"
 
tweetMsg(twt);

I'm getting the following errors:

Code:
firmware.ino: In function ‘void getPress(float)’:
firmware:222: error: invalid operands of types ‘const char*’ and ‘const char [5]’ to binary ‘operator+’
firmware:224: error: invalid conversion from ‘char’ to ‘char*’
firmware:224: error: initializing argument 1 of ‘void tweetMsg(char*)’

Can someone set me on the right track? I've tried every combination/permutation that I can think of to cast these all the same.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote a FloatToString function which permits expression of a float as a string object, with a specified number of decimal places precision.  Email me if you'd like a copy.

jrdoner@gmail.com
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char twt = "This is my test tweet. The air pressure is" + pres_c + " mb"

First, that giant string can't fit into a single character.

Second, You can't concatanate with c-strings using +. You need to use strcat() or sprintf().

Lastly, char() doesn't convert a float/int to a string. You need to use itoa() for ints or dtostrf() for floats.
Logged

Pages: [1]   Go Up
Jump to: