views:

77

answers:

4

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?

+2  A: 

I'd recommend using strtod

char *str = ...;
const char *end;
double d = strtod(str, &end);
if (*end != '\0')
{
    // questionable data
}
R Samuel Klatchko
+1: Nice example
Jon Cage
I'm amazing myself with my failure to implement your solution. char *str = myString; compiler errors there, cant convert System::String to char
Smoka
@Smoka - you need to get a `char *` or `wchar_t *` array from `System::String`. Also, if you get a `wchar_t *` you'll need to use `wcstod` instead of ``strtod`
R Samuel Klatchko
+3  A: 

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.

James McNellis
+1: Beat me to it and in more detail ;-)
Jon Cage
+2  A: 
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).

Matteo Italia
Cant seem to implement this solution either, im sure its me.Seems no matter what string i put in for yourstring in line 1it fails when strParser>>yourDouble; error reads like a type error?
Smoka
You are using C++/CLI... see the addendum.
Matteo Italia
+1  A: 

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

Jon Cage