I'm half guessing at what you want, but I think it's something like the following, where I'm assuming or changing:
ft really should be fd
- I used an
ostringstream object instead of an ostrstream
max_string_len is the size of the destination buffer in characters (including the character that will hold the '\0' terminator)
- you're not really interested in the formatted string being sent to
std::cout, but want it in the caller's provided buffer
- I changed the name of parameter
string to s so it wouldn't be confused with the std:string type.
The code:
void Time::FormatTime(char *s, unsigned int max_string_len) {
if (max_string_len == 0) {
return; // no buffer, bail out
}
std::ostringstream fd;
fd << hour << ":" << minutes;
size_t len = fd.str().copy( s, max_string_len - 1); // leave room for the null terminator
s[len] = '\0';
}
If you really don't want any part of the STL, then the following C-styled code should do the trick:
void Time::FormatTime(char *s, unsigned int max_string_len) {
snprintf( s, max_string_len, "%02u:%02u", hour, minutes);
}
It's simpler, but may C++ developers don't like using the printf() family because it's not typesafe.