Help => How to parse string in Visual C++??

W

weg22

Hi all,

The program below correctly prints out -7 in the console window:

int _tmain()
{
float test = 0.0;

String* line = S"0.1 -7";
Char chars[] = {'\t'};
String* split[] = line->Split(chars);
Console::WriteLine(split[1]);
}

How can I properly convert split[1] into a float? That is, I'd like
to do something like test = 2.0 * atof(split[1]) in order to have
-14.0 print out in the console window (without modifying the program
above).

Thanks in advance,
-weg
 
B

Ben Voigt [C++ MVP]

Hi all,

The program below correctly prints out -7 in the console window:

int _tmain()
{
float test = 0.0;

String* line = S"0.1 -7";
Char chars[] = {'\t'};
String* split[] = line->Split(chars);
Console::WriteLine(split[1]);
}

How can I properly convert split[1] into a float? That is, I'd like
to do something like test = 2.0 * atof(split[1]) in order to have
-14.0 print out in the console window (without modifying the program
above).

System::Single::TryParse

But start using C++/CLI syntax as soon as you can.
 
W

weg22

System::Single::TryParse
But start using C++/CLI syntax as soon as you can.

I tried your suggestion, but am still having trouble. Here is the
code and the error:

float accelIn = 0.0;

String* line = S"0.1 -7";
Char chars[] = {'\t'};
String* split[] = line->Split(chars);
String* s;

s = split[1];
Single::TryParse(s, accelIn);
Console::WriteLine(accelIn);

The above code generates the following error:
error C2665: 'System::Single::TryParse' : none of the 3 overloads
could convert all the argument types
 
B

Brian Muth

float accelIn = 0.0;

String^ line = "0.1 -7";
array<wchar_t>^ chars = {'\t'};
array<String^>^ split = line->Split(chars);
String^ s;

s = split[1];
Single::TryParse(s, accelIn);
Console::WriteLine(accelIn);

HTH

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top