0

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?

5
Contributors
4
Replies
5
Views
11 Years
Discussion Span
Last Post by Salem
0
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.

0

> 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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.