converting type double and int into strings

Apr 20, 2010 at 5:31pm
Hi everyone. I have a program that I used about six variables in. Five variables I used type double for and the other one I used type int. How do I convert their values into strings?

Thanks ahead of time for help
CI
Apr 20, 2010 at 5:33pm
Google works well. I found the answer in a few clicks.
Apr 20, 2010 at 5:42pm
I tried googling too. When I compiled though I would get some crazy errors telling me that what I am trying to do is illegal. I am using vs 2008. Thanks for the input.

-CI
Apr 20, 2010 at 5:47pm
Well, what code did you use?
Apr 20, 2010 at 5:49pm
@collinisaac0104
http://cplusplus.com/articles/numb_to_text/

@oghmaosiris
that depends on the googler
Apr 20, 2010 at 5:49pm
std::ostringstream strs;
strs << dbl;
std::string str = strs.str();


is their adifferent header file i need to include with this?
Last edited on Apr 20, 2010 at 5:56pm
Apr 20, 2010 at 5:50pm
Can this work you think?

std::string str = boost::lexical_cast<std::string>(dbl);
what did you use?
Apr 20, 2010 at 6:11pm
int Number = ???;
string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();

what if that number is stored in type int and we dont know exactly what it is. This would work right?
Apr 20, 2010 at 6:13pm
what do you mean "we dont know exactly what it is"?
Apr 20, 2010 at 6:27pm
within the prog, lets say double t * double r =double z and t is 3.12487894537 and r is 7894.23568, hypothetcially speaking. We dont know what z is unless we use cout<<z<<.
vs 2008 in windows form app doesnt allow cout. How can I generate what z is and put it on the windows form. I know how to put string type on the windows form app but not type int or double.
Apr 20, 2010 at 6:50pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <windows.h>
#include <sstream>

template <typename T>
  std::string NumberToString ( T Number )
  {
     std::ostringstream ss;
     ss << Number;
     return ss.str();
  }

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    int number1 = 4564;
    double number2 = 545.354;
    MessageBox(NULL, NumberToString(number1).c_str(), "Integer", MB_OK);
    MessageBox(NULL, NumberToString(number2).c_str(), "Double", MB_OK);
    return 0;
}
Topic archived. No new replies allowed.