Search code examples
c++winapitextboxc++-clicmath

how to do trig functions to data in Windows textboxes


must you convert from strings to double? if so. how?

Are there functions for trig that will accept textbox string data as is?

Is there a way to pull the data from the textbox as a numeric value, not as a string?


Solution

  • must you convert from strings to double?

    Yes.

    if so. how?

    The C++ way is to use the string streams; in particular, you'll probably want to use istringstream. A viable alternative is to follow the C way, i.e. use sscanf with the appropriate format specifier ("%f").

    Another C++ option is to use the boost lexical_cast, but starting to use boost just for lexical_cast is a bit overkill in my opinion.

    Example with istringstream:

    #include <sstream>
    // ...
    std::istringstream strParser(yourString);
    double yourDouble;
    strParser>>yourDouble;
    if(strParser.fail())
    {
        // the string couldn't be converted to a double
    }
    else if(!strParser.eof())
    {
        // the string hasn't been fully consumed; this may or may not be a problem, depending on your needs
    }
    
    Are there functions for trig that will accept textbox string data as is?

    AFAIK no, there's no need of them (although you can write them quite easily).

    Is there a way to pull the data from the textbox as a numeric value, not as a string?

    The WinAPIs provide a handful of such "convenience functions" for use in dialogs, but the only one I can recall that provides such help is GetDlgItemInt, which, as the name may suggest, works only for integers.

    Addendum

    I see now that you are using C++/CLI (you mentioned System::String): well, you should have said that, this changes the options quite a bit.

    In the managed world, your best option is to use the Double::Parse method; to catch bad-formatted strings, you should either catch the exceptions thrown by Double::Parse or call Double::TryParse before calling Double::Parse.

    Addendum bis

    Uh, I forgot, since you're using the .NET Framework probably it should be better to use the .NET trigonometric functions (class System.Math).