Solved

integer to c++ string

Posted on 2011-09-08
7
253 Views
Last Modified: 2012-06-22

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
0
Comment
Question by:Vlearns
  • 2
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 7

Expert Comment

by:Murugesan Nagarajan
Comment Utility
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
 
LVL 7

Expert Comment

by:tampnic
Comment Utility
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
 
LVL 7

Expert Comment

by:tampnic
Comment Utility
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

9 Experts available now in Live!

Get 1:1 Help Now