0

Possible Duplicate:
How to convert a number to string and vice versa in C++
how to convert from int to char*?

I am getting a user input of integers and I need to pass them to an argument - Output(char const* str); This is a Class constructor. Can you please tell me how do I do this? Thank you

Community
  • 1
  • 1
Kiran
  • 63
  • 1
  • 2
  • 8

1 Answers1

6

In C++11:

dodgy_function(std::to_string(value).c_str());

In older language versions:

std::ostringstream ss;
ss << value;
dodgy_function(ss.str().c_str());

// or
dodgy_function(boost::lexical_cast<std::string>(value).c_str());

// or in special circumstances
char buffer[i_hope_this_is_big_enough];
if (std::snprintf(buffer, sizeof buffer, "%d", value) < sizeof buffer) {
    dodgy_function(buffer);
} else {
    // The buffer was too small - deal with it
} 
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644