Is it possible to convert float to LPCSTR {aka const char*}

Feb 22, 2015 at 11:23am
closed account (9hbfjE8b)
Is it possible to convert float to char and show 3.333... instead of 3 in a message box or in a text box?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    float num1 = 10;
    float num2 = 3;
    float result = num1 / num2;

    char fresult[0];
    itoa(result, fresult, 10);

    MessageBox(NULL, fresult, "Result", MB_OK);
    return 0;
}
Feb 22, 2015 at 11:41am
Whenever I want to convert a number to string I use an std::stringstream.

1
2
3
4
std::wstringstream wss(L"");
wss << fresult;

MessageBox(NULL, wss.str().c_str(), NULL, 0);
Feb 22, 2015 at 12:22pm
closed account (9hbfjE8b)
Thank you, although I am getting an error:

error: variable 'std::wstringstream wss' has initializer but incomplete type
Feb 22, 2015 at 12:35pm
Did you #include <sstream>?
Feb 22, 2015 at 1:09pm
closed account (9hbfjE8b)
Oops... I did it now and it gives me another error:

error: cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'int MessageBoxA(HWND, LPCSTR, LPCSTR, UINT)'

My code:

#include <iostream>
#include <windows.h>
#include <sstream>

using namespace std;

int main()
{
float num1 = 10;
float num2 = 3;
float result = num1 / num2;

char fresult[0];
std::wstringstream wss(L"");
wss << fresult;

MessageBox(NULL, wss.str().c_str(), NULL, 0);
return 0;
}
Feb 22, 2015 at 1:22pm
Oh, use std::stringstream instead of std::wstringstream or use MessageBoxW() instead of MessageBox().
Last edited on Feb 22, 2015 at 1:24pm
Feb 22, 2015 at 1:27pm
closed account (9hbfjE8b)
I used std::stringstream and it gives me one more error:

error: no matching function for call to 'std::basic_stringstream<char>::basic_stringstream(const wchar_t [1])'
Feb 22, 2015 at 1:30pm
std::wstringstream wss(L"");

The L"" is a wide-char string. For a normal char string remove the L.
Feb 22, 2015 at 1:36pm
closed account (9hbfjE8b)
Ok but it doesn't show 3.333..., it shows T"u or T"y etc. Is it possible for wss.str().c_str() to print 3.333...?
Feb 22, 2015 at 2:41pm
It should do... What is T"u and T"y?
Feb 22, 2015 at 3:20pm
closed account (9hbfjE8b)
Okay, this is the code:

#include <iostream>
#include <windows.h>
#include <sstream>

using namespace std;

int main()
{
float num1 = 10;
float num2 = 3;
float result = num1 / num2;

char fresult[0];
std::stringstream wss("");
wss << fresult;

MessageBox(NULL, wss.str().c_str(), NULL, 0);
return 0;
}

now, I want the message box to show: num1 / num2, but instead it shows me the letter T,

this symbol " and one random character while it should be showing the result of num1

divided by num2 which is 3.333... (10 / 3 = 3.333...)
Feb 22, 2015 at 3:37pm
wss << fresult;

should be

wss << result;

fresult contains junk because you didn't initialize it with anything.
Feb 22, 2015 at 3:39pm
closed account (9hbfjE8b)
Yes, thank you very much, that did it, thank you sir :)
Topic archived. No new replies allowed.