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

2
  • convert integer to string, using lexical_cast. Commented Sep 5, 2012 at 6:25
  • 2
    If you're taking user input, why not simply take it as a string? No conversion needed. Commented Sep 5, 2012 at 6:26

1 Answer 1

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 } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.