Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

integer to c++ string

Posted on 2011-09-08
7
Medium Priority
?
299 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
[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
  • 2
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 36507714
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
ID: 36507723
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 9

Expert Comment

by:Murugesan Nagarajan
ID: 36508576
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
ID: 36510001
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
ID: 36513451
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

Technology Partners: 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!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

721 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