27

Possible Duplicate:
How do I convert a double into a string in C++?

I want to combine a string and a double and g++ is throwing this error:

main.cpp: In function ‘int main()’:
main.cpp:40: error: invalid operands of types ‘const char [2]’ and ‘double’ to binary ‘operator+’

Here is the line of code which it is throwing the error on:

storedCorrect[count] = "("+c1+","+c2+")";

storedCorrect[] is a string array, and c1 and c2 are both doubles. Is there a way to convert c1 and c2 to strings to allow my program to compile correctly?

Community
  • 1
  • 1
Mentalikryst
  • 327
  • 1
  • 4
  • 7

5 Answers5

72

You can't do it directly. There are a number of ways to do it:

  1. Use a std::stringstream:

    std::ostringstream s;
    s << "(" << c1 << ", " << c2 << ")";
    storedCorrect[count] = s.str()
    
  2. Use boost::lexical_cast:

    storedCorrect[count] = "(" + boost::lexical_cast<std::string>(c1) + ", " + boost::lexical_cast<std::string>(c2) + ")";
    
  3. Use std::snprintf:

    char buffer[256];  // make sure this is big enough!!!
    snprintf(buffer, sizeof(buffer), "(%g, %g)", c1, c2);
    storedCorrect[count] = buffer;
    

There are a number of other ways, using various double-to-string conversion functions, but these are the main ways you'll see it done.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 5
    I know this answer is old, but you might also want to include `std::string to_string( double );` here. – JohnJohn Feb 26 '15 at 17:13
27

In C++11, use std::to_string if you can accept the default format (%f).

storedCorrect[count]= "(" + std::to_string(c1) + ", " + std::to_string(c2) + ")";
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
23

Use std::stringstream. Its operator << is overloaded for all built-in types.

#include <sstream>    

std::stringstream s;
s << "(" << c1 << "," << c2 << ")";
storedCorrect[count] = s.str();

This works like you'd expect - the same way you print to the screen with std::cout. You're simply "printing" to a string instead. The internals of operator << take care of making sure there's enough space and doing any necessary conversions (e.g., double to string).

Also, if you have the Boost library available, you might consider looking into lexical_cast. The syntax looks much like the normal C++-style casts:

#include <string>
#include <boost/lexical_cast.hpp>
using namespace boost;

storedCorrect[count] = "(" + lexical_cast<std::string>(c1) +
                       "," + lexical_cast<std::string>(c2) + ")";

Under the hood, boost::lexical_cast is basically doing the same thing we did with std::stringstream. A key advantage to using the Boost library is you can go the other way (e.g., string to double) just as easily. No more messing with atof() or strtod() and raw C-style strings.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
  • Actually, `boost::lexical_cast` does not use `std::stringstream` under the hood. It implements its own conversion routines which are significantly faster than the using `stringstream` and in most cases are faster than `scanf`/`printf`. See: http://www.boost.org/doc/libs/1_48_0/doc/html/boost_lexical_cast/performance.html – Ferruccio Dec 09 '11 at 11:47
  • The source for `lexical_cast` looks much different from when I last saw it. It appears they've improved performance considerably in the past couple of Boost releases. That's even more reason to go with it if you can. – Michael Kristofik Dec 09 '11 at 13:51
10
std::string stringify(double x)
 {
   std::ostringstream o;
   if (!(o << x))
     throw BadConversion("stringify(double)");
   return o.str();
 }

C++ FAQ: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1

aJ.
  • 34,624
  • 22
  • 86
  • 128
1

I believe the sprintf is the right function for you. I's in the standard library, like printf. Follow the link below for more information:

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

mp.
  • 182
  • 1
  • 3
  • 8
    `sprintf` is unsafe for number formatting, because it doesn't do any bounds checking on the output buffer, and there's no good portable way to determine the size of the buffer that would be safe for any `double` value. – Pavel Minaev Jul 14 '09 at 03:23
  • Well in that case, use `snprintf` :) But indeed, predicting the needed buffer size can be hard. – tomsmeding Aug 27 '13 at 13:38