Hi All,
In C++ how do I convert the string value position to a integer. The compiler keeps on giving me an error.
string numFamilyMembers[100];
if(atoi(numFamilyMembers[0]) > 5){
special=0;
}
Printable View
Hi All,
In C++ how do I convert the string value position to a integer. The compiler keeps on giving me an error.
string numFamilyMembers[100];
if(atoi(numFamilyMembers[0]) > 5){
special=0;
}
Thanks Paul, for replying to my forum
The atoi function expects a 5
The compiler returns the following error:
main.cpp:46: error: cannot convert `std::string' to `const char*' for argument `1' to `int atoi(const char*)
I'm not sure what the std::string::c_str() returns.
Regards,
Stephen
look it up: www.cplusplus.com. ;) atoi expects a const char *, you are giving it a std::string.
No, I didn't ask what value you're sending the function. I asked the parameter type. The type that atoi() expects is a const char *, therefore your parameter must have that type -- const char *. A 5 is not a constant char pointer.
The c_str() function returns a const char*, and that is what atoi( ) is expecting.
http://www.cplusplus.com/reference/c.../cstdlib/atoi/
http://www.cplusplus.com/reference/string/string/c_str/
I replied in the way I did in my original post because whatever the function is that you're calling, you must know the parameter type for each parameter. Otherwise you're not understanding how to call any function when certain parameter types are specified. Always understand what the function you're calling expects as arguments -- don't guess, as everything is (or should be) documented.
Regards,
Paul McKenzie