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

raja
  • 65
  • 2
  • 5
  • 5
    possible duplicate of [How to get a char* pointer to a C++ string?](http://stackoverflow.com/questions/2707980/how-to-get-a-char-pointer-to-a-c-string) – In silico Oct 10 '10 at 10:03

4 Answers4

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.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • 1
    If you put in a check that the conversion actually succeeded, I'd up-vote. – sbi Oct 10 '10 at 10:16
  • @sbi: that depends on what is success "123abs"->123. Is it success? or you mean only complete match is a success? – Armen Tsirunyan Oct 10 '10 at 10:19
  • 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. – rubenvb Oct 10 '10 at 11:32
  • @Armen: That's debatable, but _at the very least_ make sure that `"abc"` is caught! – sbi Oct 10 '10 at 13:13
  • @sbi: well. after the in >> cc; you add a line if(in.fail()) {/*process error*/} pretty much the same way as with console input. – Armen Tsirunyan Oct 10 '10 at 13:15
  • @rubenvb: I strongly disagree. Usually, when you need to parse strings, you're reading either from the disk or from the user. In both cases, the CPU isn't the bottleneck. Using stringstreams is indeed the simplest solution available with just the std lib at hand and very recommendable for novices. – sbi Oct 10 '10 at 13:20
  • @Armen: _I_ know. It's those who ask these questions that don't. – sbi Oct 10 '10 at 13:23
  • @sbi: what's wrong with: `string s("123abc"); int i=atoi(s.c_str()); if( i== 0 || i == INT_MAX || i == INT_MIN ) cout << "error occurred" << endl;` ? – rubenvb Oct 10 '10 at 13:51
  • @Armen: I had already up-voted when I saw that you had added the text below. `:)` (I now saw that you even added that _before_ I wrote my comment. I'm not sure how I missed that. Maybe you did this after I loaded this page, but before I posted my comment.) – sbi Oct 10 '10 at 14:26
  • @sbi: a simple `if( s.size()>0 && '0' == s[0] ) int i=0;` would suffice. I agree gets a bit lengthy, but at least you know that there's not too much going on behind the scenes that is unnecessary. – rubenvb Oct 10 '10 at 15:21
  • 1
    @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... – sbi Oct 10 '10 at 16:01
2

According to your description, maybe what you want is:

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

xueyumusic
  • 229
  • 2
  • 9
  • 1
    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 –  Oct 10 '10 at 13:39
1

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

usta
  • 6,699
  • 3
  • 22
  • 39
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.

Daniel Lidström
  • 9,930
  • 1
  • 27
  • 35
  • You're a great boost fan and expert eh? :))) – Armen Tsirunyan Oct 10 '10 at 10:14
  • 1
    @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 Tsirunyan Oct 10 '10 at 10:16
  • @Armen: Yes I most definitely am! :-) A fan, that is! – Daniel Lidström Oct 10 '10 at 10:44
  • 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) – rubenvb Oct 10 '10 at 11:33
  • 1
    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. – sbi Oct 10 '10 at 13:21