Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-22
10
Medium Priority
?
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 1500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Simple Linear Regression
Progress

610 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