Solved

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

Posted on 2006-11-22
10
330 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now