I'm currently working with MFC to build a certain program, which requires the user to enter a series of numbers as highlighted down below in a CString
( let's call it aCString for simplicity).
I can convert a string or array of chars to an array of floats using 'strtok' without problems .
But I'm struggling to convert CString
to a string or array of chars so I can do the pre-mentioned conversion !
-I tried strcpy
strcpy(my_string, (LPCTSTR)aCString);
But got that error
char *strcpy(char *,const char *)': cannot convert argument 2 from 'LPCTSTR' to 'const char *'
I appreciate the help !
The CString class template provides the Tokenize member, that can be used to split an input string into individual tokens. The tokens can then be converted to floating point values using the std::stof function:
std::vector<float> ToFloats( const CString& numbers ) {
std::vector<float> buffer;
int start{ 0 };
CString token = numbers.Tokenize( _T( "," ), start );
while ( start != -1 ) {
buffer.push_back( std::stof( { token.GetString(),
static_cast<size_t>( token.GetLength() ) } ) );
token = numbers.Tokenize( _T( "," ), start );
}
return buffer;
}