-
January 22nd, 2011, 10:26 AM
#1
Convert String Value to an Int
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;
}
-
January 22nd, 2011, 10:29 AM
#2
Re: Convert String Value to an Int
Originally Posted by stephenalistoun
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;
}
What is the parameter type that atoi( ) expects? What does the std::string::c_str() function return?
Regards,
Paul McKenzie
-
January 22nd, 2011, 10:51 AM
#3
Re: Convert String Value to an Int
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
-
January 22nd, 2011, 11:05 AM
#4
Re: Convert String Value to an Int
look it up: www.cplusplus.com. atoi expects a const char *, you are giving it a std::string.
-
January 22nd, 2011, 11:19 AM
#5
Re: Convert String Value to an Int
Originally Posted by stephenalistoun
Thanks Paul, for replying to my forum
The atoi function expects a 5
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
Last edited by Paul McKenzie; January 22nd, 2011 at 11:25 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|