开发者

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?


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

Yes. There are quite a few ways to do this.

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

No, but if you really wanted one, you could easily implement such a function using one of the techniques described in the aforementioned question.

Usually you want to validate user input to be sure it is correct before you use it, and validation of the input in numeric form is much easier than validation of the raw string.

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

Maybe; it depends entirely on what GUI framework you are using.


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


I'd recommend using strtod

char *str = ...;
const char *end;
double d = strtod(str, &end);
if (*end != '\0')
{
    // questionable data
}


There's no way to do a straight conversion from textbox to double as far as I know. You'll need to first convert to a double with something like strtod.

You could easily make a generic function which accepts a textbox control as an argument and returns a double (assuming it's the textbox contains a valid number) mind you..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜