C++ - adding a double/int to a char array / string


I'm strictly an asp/vb developer but a current project requires that i work with some C++ code. I have what i imagine is a really simple problem, but can't seem to get round it.

i have a function that takes a string/char array as input:

DansFunc("hello world");

i also have a value stored in a double:

double dansValue = 123456;

what i want to do is add the value in the dansValue double to the char array / string that is sent to DansFunc. so in the above example it will result in:
DansFunc("hello 123456 world")

in vb this would just be DansFunc("hello " & dansValue & " world")

can anyone tell me how to do the equivalent in C++ ???
Please also bear in mind that you will need to give me the code to include any needed headers, as i won't know what they are.

This is really urgent, so 500 points up for grabs.
Who is Participating?
adg080898Connect With a Mentor Commented:
There is a way to do it without using a ton of elephant-sized libraries (that might require you to play with the linking of the app - not easy if you don't know C++):

Create a function that takes string, double, string.

void DansNumInserter(char *outbuf, unsigned maxsize, const char *bef, double n, const char *aft)
   _snprintf(outbuf, maxsize, "%s %lf %s", bef, n, aft);

Then, where you want the stuff merged:


char mybuf[256];
DansNumInserter(mybuf, 256, "Hello", 123456.0, "world");


Where 256 could be larger or smaller depending on need. If it needs to be huge, do this:


char *mybuf;
mybuf = malloc(16384); // or bigger...
DansNumInserter(mybuf, 16384, "Hello", 123456, "world");
... use the string...
free(mybuf); // don't need built string anymore


Note that you would need to #include <stdio.h> in the source file where DansNumInserter is (preferably at the top of the source file, where there are probably other #include statements.
hi Dan,

i'm not sure if you want the program to find the first space and then enter the double value in there. if so let me know. i'm assuming that you just want to convert the double to a string and want to add the independent strings.


#include <sstream>
#include <iostream>

using namespace std;

#include <iostream>
#include <sstream>

using namespace std;

int main()

/// this is what you need
stringstream ss;
double dansVal = 123456;
ss << "hello " << dansVal << " world";

///end this is what you need

cout << "the string you want is [" << ss.str() <<"]"<<endl;

// DansFunc(ss.str()); //is how you want to call your function

return 0;

If you are using .NET go with:

//put this after your includes
using namspace System;

//in your code
String^ s = "asdf";
double d = 123493209;

s = s + System::Convert::ToString(d);
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Oh the equiv in .NET would be:

using namespace System;

DansFunc("hello " + System::Convert::ToString(dansValue) + " world")
DanM711Author Commented:
chhorka_expert, tried your approach. When i try to build i get a list of over 20 errors, most of which resemble the following:
   error LNK2005: __configthreadlocale already defined in msvcrt.lib(MSCVR80.dll))
   error LNK2005: __encode_pointer already defined in msvcrt.lib(MSCVR80.dll))
Hi Dan, what's the first error you get? And what version of Visual Studio are you running?
DanM711Author Commented:
YoungBonzi, i get the following error:

error C2871: 'System' : a namespace with this name does not exist.
Okay, so you get that with my code...is this a console application, are you running Visual Studio 6? And could we see your code?
hello, try this:

#include <iostream>  // for std::cout
#include <sstream>   // for std::stringstream
#include <string>      // for std::string

void DansFunc( std::string s )
      std::cout << s;

int main( int argc, char* argv[] )
      double DansValue = 12345;
      std::string DansString( "Hello World" );
      std::string::size_type loc = DansString.find( " ", 0 );    // get location of " "
      std::stringstream ss;
      if( loc != std::string::npos ) {
            ss << DansValue;    // cconvert double to string
            DansString.replace( loc, 1, " " + ss.str() + " " );    // replace " " with " 12345 "
      DansFunc( DansString );
      return 0;
DanM711Author Commented:
I'm awarding the points to adg as he gave an answer that was closest to the only code i managed to get working which was as follows:

char  buffer[200];
double value(123.4);
sprintf(buffer, "the answer is: %f", value);

thanks for your suggestions everyone.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.