string strd1 = "9";
int m=atoi(strd);

Got the error: error C2664: 'atoi' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'


how to change the second line to make it work?

change it to this and you wont have a problem.

char strd[] =  {'9', '\0'};
int m=atoi(strd);
string strd1 = "9";
int m=atoi(strd);

Got the error: error C2664: 'atoi' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'


how to change the second line to make it work?

atoi doesnt accpet a C++ style string but a C style string which si terminated with a null character.

Try something like:

string my_number = "1234" ;
int number = atoi( my_number.c_str( ) ) ;

It should get teh work done for you.

Member Avatar for iamthwee

i wouldn't use atoi. Try stringstream.

commented: Absolutely the way to go in c++ :) +3

> i wouldn't use atoi. Try stringstream.
Yes! use a string stream if you're using C++.

If you must use a C function, at least use strtod() which has some error checking capabilities.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.