0

I was wondering if there was an alternative to OutputDebugString but for floats instead? As I want to be able to view in the output in Visual studio the values.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
DorkMonstuh
  • 819
  • 2
  • 19
  • 38

2 Answers2

2

First convert your float to a string

std::ostringstream ss;
ss << 2.5;
std::string s(ss.str());

Then print your newly made string with this

OutputDebugString(s.c_str());

Optionaly you can skip the intermediate string with

OutputDebugString(ss.str().c_str());
Eric
  • 19,525
  • 19
  • 84
  • 147
  • 1
    But OutputDebugString() does not accept a [const char *] as an input! It needs to be converted to LPCWSTR first. – mohaghighat Mar 14 '17 at 23:15
0

I combined Eric's answer and Toran Billups' answer from https://stackoverflow.com/a/27296/7011474 To get:

std::wstring d2ws(double value) {
    return s2ws(d2s(value));
}

std::string d2s(double value) {
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

double theValue=2.5;
OutputDebugString(d2ws(theValue).c_str());

Edit: Thanks to Quentin's comment there's an easier way:

std::wstring d2ws(double value) {
    std::wostringstream woss;
    woss << value;
    return woss.str();
}

double theValue=2.5;
OutputDebugString(d2ws(theValue).c_str());
S. Pan
  • 619
  • 7
  • 13