Solved

integer to c++ string

Posted on 2011-09-08
7
271 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 500 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 8

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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