1

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

enter image description here

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 !

2
  • 1
    The equivalent to strtok() (although not destructive) is CString::Tokenize(). Commented Oct 24, 2016 at 12:55
  • Due to internationalization CString isn't using normal char as characters, but TCHAR which will be defined as char or wchar_t depending on the UNICODE macro in your project settings. Commented Oct 24, 2016 at 13:03

1 Answer 1

4

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Don't forget exception handling for std::stof
@BarmakShemirani: It's odd that you single out std::stof. std::vector::push_back can throw, and so does CStringT::Tokenize. But that's all besides the point: The appropriate way to handle exceptions in this function is to not do anything at all. This function cannot know, whether failure to convert the input, or construct the output is tolerable or fatal. It's the caller that needs to handle exceptions, because only the caller knows.
@BarmakShemirani: This function promises to return a vector of floats. If it cannot satisfy that promise, it throws an exception. Any caller up the call chain can decide, whether it can reasonably handle exceptions. This function cannot. Besides, uncaught exceptions do not "crash". They call the unexpected handler, which by default calls terminate. That's a clean and deterministic shutdown.
Thanks IInspectable, I saw your comment in a different section, I have to read up on this more. The problem would be solved simply by adding try/catch(...) for std::stof. I don't want to close the program if the user accidentally pushes the wrong key!
@BarmakShemirani: That is the wrong solution. It merely hides bugs. You may not like your program to terminate on invalid input, but you surely don't want your program to use invalid input, without even knowing about it. If you need to continue running, catch the exceptions somewhere higher up in the call chain. This function cannot (and must not) hide the error. It is there for a reason.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.