How to convert an INTEGER to STRING - C++ Linux

This is my program
******************
#include <iostream>
using namespace std;

int main()
{
   int i = 0;
   string message = "";
   // message = the value of "i" converted to string.
   return 1;
}
********************

I want to convert "i" to string, how ? Please remember to add the class that I need to add...
probineAsked:
Who is Participating?
 
stefan73Commented:
probine,
> `itoa' undeclared (first use this function)

> Any suggestion ? Do I need to incude some class ?

itoa() is C API. Just do:

#include <stdlib.h>
0
 
william_jwdCommented:
string message = itoa(i);
0
 
probineAuthor Commented:
Hello william_jwd.

I got this after using your code:

`itoa' undeclared (first use this function)

Any suggestion ? Do I need to incude some class ?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
stefan73Commented:
Hi probine,
You could use stringstream:

std::stringstream my_stringstream;
my_stringstream <<  i;

message = my_stringstream.str();

Cheers,

Stefan
0
 
william_jwdCommented:
you should include stdlib.h
0
 
probineAuthor Commented:
My program:
********************
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
   int i = 0;
   string message = "";
   message = itoa(i);
   return 1;
}
**********************

When trying to compile with:   g++ test.cpp -o test  I get this error:
**********************
`itoa' undeclared (first use this function)
0
 
probineAuthor Commented:
Hi stefan73.

When trying to compile with streamstring I get this:

aggregate `std::stringstream my_stringstream' has incomplete type and cannot be defined

Do I need to include any specific class ?
0
 
waysideCommented:
What compiler are you using?

For Microsoft, the function is

char *_itoa( int value,  char *string,  int radix);

Note the underscore.

Parameters:

value :Number to be converted.
string :String result.
radix :Base of value; must be in the range 2 – 36.

So do something like


int i = 123;
char buf[100];
_itoa(i, buf, 10);


buf now contains the string.
0
 
probineAuthor Commented:
FORGET IT..... I SAID LINUX ... Let me ask this now:

I have a program:

char * message;
string temp="HELLO"

How do I put the value "HELLO" in message ?
0
 
stefan73Commented:
probine,
> Do I need to include any specific class ?

Yes, <sstream>
0
 
waysideCommented:
> FORGET IT..... I SAID LINUX

Sorry, I missed that in the title.

> How do I put the value "HELLO" in message ?

message = temp.c_str();

0
 
waysideCommented:
It needs to be const unless you cast it:

const char *message = temp.c_str();

or

char *message = (char *)temp.c_str();

The second method is dangerous if you plan on changing the contents of message.
0
 
stefan73Commented:
> itoa() is C API. Just do:

Ouch, it's atoi, not itoa...

For itoa, you'll need sprintf(), which is not the C++ way to do it:


char* itoa(int input){
    static char buffer[16];

    snprintf(buffer,sizeof(buffer),"%d",input);

    return buffer;
}

(using snprintf here to avoid buffer overflows)
0
 
probineAuthor Commented:
To WAYSIDE:

While trying to compile:

invalid conversion from `const char*' to `char*'

0
 
waysideCommented:
Can you post your code?
0
 
probineAuthor Commented:
My code is simple:
******************
#include <iostream>
using namespace std;

int main()
{
   char * message;
   string temp = "HELLO";
   // Here "message" should get the word "HELLO" .... how ?
   return 1;
}
**********************

How do I make my "message" to get the value of temp, so when I say:

cout << message

then I get "HELLO"    ?
0
 
stefan73Commented:
probine,
> How do I make my "message" to get the value of temp, so when I say:

> cout << message

> then I get "HELLO"    ?

message=temp.c_str();
0
 
william_jwdCommented:
Try this,

#include <iostream>
using namespace std;

int main()
{
   char * message;
   string temp = "HELLO";
   strcpy(message, temp);
   // Here "message" should get the word "HELLO" .... how ?
   return 1;
}
0
 
probineAuthor Commented:
Hi  william_jwd, I compile your program and I got this in complation time:

 cannot convert `std::string' to `const char*'

0
 
ppk1981Commented:
Hi,

there are tooo many queries here ..

what about this ...

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
      int i = 10;
      string message = "";
      char IntStr[10] ;

      sprintf( IntStr, "%d", i ) ;
      message = IntStr;
      return 1;
}
0
 
ppk1981Commented:
Hi,

there are tooo many queries here ..

what about this ...

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
      int i = 10;
      string message = "";
      char IntStr[10] ;

      sprintf( IntStr, "%d", i ) ;
      message = IntStr;
      return 1;
}

praveen
0
 
ppk1981Commented:

I am Sorry .. I repeated it twise
0
 
__Julien__Commented:
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string temp = "HELLO";
   char* message = new char[temp.length() + 1];

   strcpy( message, temp.c_str() );
   cout << message;

   delete [] message;
   return 1;
}
0
 
rstaveleyCommented:
Wow what a confusing thread this has turned into.

Here's what you need:
--------8<--------
#include <iostream>
#include <sstream>

int main()
{
        int i = 0;
        std::ostringstream ostr;                                      // This is an output stream - cum - string
        ostr << i;                                                          // Output the number to the string stream
        std::string str = ostr.str();                                  // Get the output string
        std::cout << "Your string is \"" << str << "\"\n";  // Bob's your uncle
}
--------8<--------
0
 
lyvanbaoCommented:
In fact, the itoa function is not defined in ANSI C, but supported by some compilers like MSVC, but not g++ (see http://www.cplusplus.com/ref/cstdlib/itoa.html).

You can write your self this itoa function. In the following example, ___char *buff___ must be allocated before calling of this function.

char * itoa(int n, char *buff, int radix)
// convert a positive integer n to char *buff
// for instant, this function work with radix <= 10;
// a little change to run with radix > 10
{
      int q, r;
      int i = 0;
      char tmp[33];  // for radix = 2 and 32 bits computer
      do{
            q = int(n / radix);
            r = n % radix;
            n = q;
            tmp[i++] = 48 + r;
      }while(q > 0);
      int j;
      for(j = 0; j < i; j++){
            buff[j] = tmp[i - j - 1];
      }
      buff[j] = NULL;
      return buff;
}

Hope enjoy it.

0
 
evilrixSenior Software Engineer (Avast)Commented:
More C++ like (not necessarily better)...

// What we want to convert
int i = 10;

// String stream to facilitate conversion
std::stringstream ssData;

// Convert to string (via stream)
ssData << i;

Of course, there is also boost::lexical_cast!
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.