Solved

integer to c++ string

Posted on 2011-09-08
7
255 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
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 7

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to save 10 165
convert char array to number in c 5 85
How to print into std::string like sprintf using C++11? 12 60
Android development question 2 37
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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