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
  • 49,394
  • 10
  • 79
  • 128
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,538
  • 21
  • 26
Rosme
  • 1,019
  • 10
  • 23
1

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

Simon
  • 29,389
  • 8
  • 76
  • 90
zeller
  • 4,774
  • 2
  • 20
  • 38