You should only really call a function like .c_str if you need to call a legacy C function. In this case atoi is a legacy C function but C++ has plenty of better options that you could use (actually the legacy C standard library has better options to use other than atoi). In a good C++ design you should be avoiding dropping down to C constructs like arrays if at all possible favouring vector<> or array<> instead.
This can be implemented entirely using the C++ library using a stringstream which has the advantage that you cen tell how much of the string has been converted.
-
#include<iostream>
-
#include<string>
-
#include<sstream>
-
-
using namespace std;
-
-
int main()
-
{
-
string str = "131.90";
-
string leftover;
-
istringstream iss;
-
int result;
-
-
iss.str(str);
-
-
iss >> result;
-
iss >> leftover;
-
-
cout << "Converted: " << result << " Leftover: " << leftover << endl;
-
}
-
Output: Converted: 131 Leftover: .90
By simply changing the type of result to double I can convert the whole string.