Hey everyone,
I'm trying to figure out how to convert from an Integer or a Double into a string.

I want a string variable to print out on a command line.

Thanks for any help.

- WolfShield

Recommended Answers

All 8 Replies

t

Why convert to string? If you want to print to terminal, just output it to cout, as in:

int i = 999;
cout << "i == " << dec << i << endl;

Yeah,
thanks!

- WolfShield

There is a functional called itoa which can convert int into strings

Awesome,
Thank you!

- WolfShield

There are a lot of options, including C-style sprintf(), C++ ostringstream objects, etc. FWIW, itoa() is not a standard function, but a lot of systems and compilers support it - caveat programmer! Anyway, here is a link to some documentation on the ostringstream class which is probably what you should use: http://www.cplusplus.com/reference/iostream/ostringstream/

@rubberman

Thanks for the correction

@rubberman

Thanks for the correction

Not a problem. We all get used to the goodies on our particular systems, often not considering if they are available elsewhere. After 30 years of serious cross-platform development (one code base, 10+ operating systems, 10M lines of code) I quickly learned that what works on one system, may either not work at all (not available), or function differently on another. It gets really fun when you are bit-twiddling and you switch from a big-endian to a little-endian (or vice versa) machine, or one where a long integer is 32 bits to one where it is 64 bits.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.