6 years, 4 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

6 years, 4 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;

Accepted Answer

ahhh I did this but I put the char array in line 3 of my code inplace of calc which didnt work. Ive changed it so now it makes the string in printf and now its working great. thanks

posted by Nick Oliver 04 Sep 2015

hi

posted by Saad Alsebaiee 06 Apr 2017