Solved

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

Posted on 2006-11-22
10
334 Views
Last Modified: 2010-04-16
Hi,

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.
0
Comment
Question by:DanM711
10 Comments
 
LVL 4

Expert Comment

by:chhokra_expert
ID: 17998914
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.

hth!
kage

#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;
}


0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17999316
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);
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17999329
Oh the equiv in .NET would be:

using namespace System;


DansFunc("hello " + System::Convert::ToString(dansValue) + " world")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DanM711
ID: 17999837
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))
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17999847
Hi Dan, what's the first error you get? And what version of Visual Studio are you running?
0
 

Author Comment

by:DanM711
ID: 17999874
YoungBonzi, i get the following error:

error C2871: 'System' : a namespace with this name does not exist.
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17999900
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?
0
 
LVL 4

Expert Comment

by:Raymun
ID: 18000386
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;
}
0
 
LVL 8

Accepted Solution

by:
adg080898 earned 500 total points
ID: 18009885
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.
0
 

Author Comment

by:DanM711
ID: 18024166
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);
DansFunc(buffer)

thanks for your suggestions everyone.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question