integer to c++ string


i have a integer valu and i want to add it to a string

so i1 = 3
i2 = 4

i want to create a c++ string such that "3,4", how do i do it
VlearnsAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
The simplest way might be using a std::stringstream, e.g.
#include <string>
#include <sstream>
using namespace std;

string make_it_a_string(const int i1, const int i2) {

  stringstream ss;

  ss << i1 << ',' << i2;

  return ss.str();
}

Open in new window

0
 
jkrCommented:
Oh, and since someone is going to mention that there's 'sprintf()' as well, let's cover that angle also:
int i1 = 3;
int i2 = 4;
char buf[256]; // that's definitely too big ;o)

sprintf(buf,"%d,%d",i1,i2);

Open in new window

0
 
Murugesan NagarajanSubject-matter expert at C++ C delivery, implementation, at UNIX oriented operating systems (Windows: CYGWIN_NT MINGW32_NT MINGW64_NT)Commented:
Updated code:

#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;

/*
        Add comments for the function usingBuffer
*/
char *usingBuffer( const int param1, const int param2)
{
        static char numBuffer[512] = { '\0'} ; /* Initialize at the definition. Related to valgrind reports */
        memset( numBuffer, '\0', sizeof(numBuffer)) ;
        if ( param1 && param2 ) /* Exception handling */
        {
                sprintf( numBuffer, "%d,%d", param1, param2);
        }
        else
        {
                sprintf( numBuffer, "The value passed to the function usingBuffer are param1 [%d] param2[%d]\n", param1, param2) ;
        }
        return numBuffer ;
}

/*
        Add comments for the function make_it_a_string
*/
string make_it_a_string( const int param1, const int param2)
{
        stringstream ss(stringstream::in | stringstream::out) ; /* Initialize at the definition. Related to valgrind reports */
        if ( param1 && param2 ) /* Exception handling */
        {
                ss << param1 << ',' << param2;
        }
        else
        {
                ss << "The value passed to the function make_it_a_string are:\n" ;
                ss << "Parameter1:\t" << param1 << "\n" ;
                ss << "Parameter2:\t" << param2 << "\n" ;
        }
        return ss.str();
}

/*
        Add comments for the function main
*/
int main ()
{
        char *outBuff = NULL ; /* Initialize at the definition. Related to valgrind reports */
        string outNumString( "") ; /* Initialize at the definition. Related to valgrind reports */
        outNumString = make_it_a_string( 0, 0) ;
        if ( outNumString.length() ) /* Exception handling */
        {
                cout << outNumString.data() << "\n" ;
        }
        else
        {
                cout << "outNumString.length() is zero" << "\n" ;
        }
        outBuff = usingBuffer( 0, 0) ;
        if ( outBuff ) /* Exception handling */
        {
                cout << "outBuff:" << outBuff << "\n" ;
        }
        else
        {
                cout << "outBuff is null\n" ;
        }
        return 0 ;
}

Open in new window

0
 
tampnicCommented:
If the application is already using MFC or ATL there is also CString::Format() which accepts sprintf style formatting strings.

 
CString the_string(_T("Some text here"));
int i1,i2
i1 = 3;
i2 = 4;
the_string.Format(_T("%s - %d,%d"),the_string,i1,i2);

Open in new window


Not worth adding MFC or ATL just to use some string handling routines though. I would go with jkr and use sprintf - its standard C and if using Microsoft VC++ there is the secure version sprintf_s to avoid buffer overflow exploits. The Boost::format library also gives string formatting functionality.

You shouldn't be writing new functions to do this - there are library functions available which already do this and have been thoroughly debugged over many years of use by many thousands of programmers. Call a library function!

Cheers,
  Chris
0
 
tampnicCommented:
Evilrix: In my limited experience some questioners seem to have trouble putting questions in the right zones so I thought I should mention the MFC variant for completeness. Can we rely on questioners only asking for pure ANSI C++ questions in the "C++ Programming Language" zone? I'm fairly new here so would appreciate clarification. Do moderators screen the questions and put them in the right zones?

The original question didn't give a specific platform but as most of us are coding for the Windows platform I thought it was worthwhile to mention sprintf_s as its use should be encouraged if its available. I advised the questioner to use jkr's solution - don't think that is point grubbing!

Cheers,
  Chris
0
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.