1

i am getting an problem

 string ccc="example"; int cc=atoi(csession); 

it says cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’ do i should convert the string to char array and then apply to atoi or is there is any other way

1

4 Answers 4

3
istringstream in(ccc); int cc; in >> cc; if(in.fail()) { // error, ccc had invalid format, more precisely, ccc didn't begin with a number //throw, or exit, or whatever } 

istringstream is in header <sstream> and in namespace std. The above code will extract the first integer from the string that is, if ccc were "123ac" cc would be 123. If ccc were "abc123" then cc would have undefined value and in.fail() would be true.

Sign up to request clarification or add additional context in comments.

11 Comments

If you put in a check that the conversion actually succeeded, I'd up-vote.
@sbi: that depends on what is success "123abs"->123. Is it success? or you mean only complete match is a success?
Although it is a full-blown C++ solution, it is waaay to heavy on resources to be justifiably used in this case. atoi is better IMHO.
@Armen: That's debatable, but at the very least make sure that "abc" is caught!
@rubenvb: Even if we forget the fact that this, now, isn't simple anymore, no, it wouldn't suffice, since this would fail to read "000". And that's just the first thing I could think of. Face it, doing this using the C std lib is hard for beginners, doing it using the C++ std lib (i.e. using string streams) is much easier. And the speed is rarely ever needed. Premature optimization is the root...
|
2

According to your description, maybe what you want is:

string ccc="example"; int cc=atoi(ccc.c_str());

1 Comment

This is the best solution. Exactly what I would use in this case. In some cases I would also use strtol if I need the end unconvertible character in the string. Using a temporary stream to convert std::string into int is ridiculous. +1
1

Use .c_str() on the string object to pass it to atoi

Comments

1

Hehe, nice one Armen. Here's a solution using boost::lexical_cast:

#include <boost/lexical_cast.hpp> . . . int c = boost::lexical_cast<int>(csession); 

Documentation available here: boost::lexical_cast.

5 Comments

You're a great boost fan and expert eh? :)))
@raja: Daniel's solution is much better in terms of "prettiness" and ease of use. However, if you don't have boost on your machine, or don't want to waste (WASTE?!!!:) time to download and install it, I believe my version is still better that that of using atoi.
@Armen: Yes I most definitely am! :-) A fan, that is!
or: "how to pull in a huge dependency to replace functionality already present..." (don't get me wrong: boost is great, but for small things like this, you don't need it)
I, too, believe that this is a very good solution. No need to check for all the possible error conditions, as the guys at boost have already done all this for you. What more could you ask for? +1 from me. If you haven't installed boost yet, go ahead and do so.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.