1

I'm trying to get my Frames-Per-Second readout to appear in my window title. I have done it once before, but how would I set up the code to do that? I need to switch from float to const char *.

genpfault
  • 51,148
  • 11
  • 85
  • 139
cryfreq
  • 13
  • 3
  • Converting float/double to string is here: http://stackoverflow.com/questions/1123201/convert-double-to-string-c – Tim May 20 '12 at 19:20

2 Answers2

3

A simple way to do, and making it compatible with every numerical could be that:

#include <sstream>

template<class T>
char* toChar(T t) {
    std::ostringstream oss;
    oss << t;
    return oss.str().c_str();
}

This way, no matter if you use int, float, long or whatever else, it will work and return it as a char* string.

Keith Layne
  • 3,688
  • 1
  • 24
  • 28
Rosme
  • 1,049
  • 9
  • 23
1

You can use an istringstream then str() then c_str().

Simon
  • 31,675
  • 9
  • 80
  • 92
zeller
  • 4,904
  • 2
  • 22
  • 40