Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 2 months ago.
Converting Int To string to combine with other strings
Hi, I know this has been answered before but I still have a problem with it.
I am writing a library and one of the methods does a calculation and then returns that number within a string. The calculation isnt important and is more then 1 line but it returns an int, normally between 0 and 9.
for example:
Current Function
string mycalc(){ int calc = <calculation here>; string message = "The Result Is: " + calc + "V."; //Error on this line return message. }
This code retuens me the error: Expression must have integral or enum type
Ive see methods where the int is converted into a char array but when the char array replaces the string message I still get the error message.
Is there a way to do this?
Thanks
1 Answer
9 years, 2 months ago.
One way is - allocate a temporary buffer char buf[50]; -use sprintf : example = sprintf(buf,"Bat 2 %5.0fmA %4.0fmAh", getBattC(2),rc.batt.cb2); - convert this buffer to a string string s = buf;