• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3453
  • Last Modified:

Convert CString to float!!

m_gVal is a Cstring
float num = atof(m_gVal);

This code does not work since it loads not the value instead some other value, I am geussing where the data is being loaded from, thats the value we got.

after running the program the value of m_gVal = {0x00325ff0 "0"} so I want the value in between the "" 

thanks and please do not refer to websites, please fix what my problem is!
0
killaz4life
Asked:
killaz4life
  • 4
  • 3
  • 2
  • +3
3 Solutions
 
jkrCommented:
Try

float num = atof(m_gVal.GetBuffer(0));

0
 
killaz4lifeAuthor Commented:
I did, but I still keep getting that stupid value, not getting the value i am inputting?
0
 
jkrCommented:
How exactly are you trying to do that conversion? (in terms of code ;o)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
killaz4lifeAuthor Commented:
u got it up there, wut i wrote!
0
 
mahesh1402Commented:
just a quick'n dirty solution:

CString m_gVal = _T("123.456");
float num;
_stscanf(m_gVal, _T("%f"), &num);


-MAHESH
0
 
Jase-CoderCommented:
Hello,

try:       

CString T = "123.456";
float Z = atof(T.GetBuffer(T.GetLength()));
0
 
AndyAinscowCommented:
     CString s(_T("{0x00325ff0 \"123.45\"}"));
      int n = s.Find(_T('"'));
      CString s1(s.Right(s.GetLength() - n - 1));
      float num = _tstof(s1);

num now has the value 123.45
0
 
AndyAinscowCommented:
ps
CString s(_T("{0x00325ff0 \"123.45\"}"));  // s = {0x00325ff0 "123.45"}

also note the _tstof instead of atof (copes with ANSII and UNICODE conversion, atof won't cope with UNICODE)
0
 
AndyAinscowCommented:
Extract from help
The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The function stops reading the input string at the first character that it cannot recognize as part of a number.

Your string had a { as the first character - so the conversion would stop there and return 0 as there was no number specified.
What I did was to find the first instance of " in your string as you said the number you wanted converted started after that, then prepare a second string (for clarity - not necessarily good programming) and the conversion to the float was performed on the second string.
0
 
killaz4lifeAuthor Commented:
I tried what you said and it didnt make any difference, i get something called bad pointer!
But the example you got there any is the right 1!!! You understood the question right your on the right path. I honestly do not know why this is happening. Plus I can assure you, i didnt make those values, the first value came from no where?
0
 
AndyAinscowCommented:
I'm puzzled.
In your question you say the string is {0x00325ff0 "0"}, now you say you are getting a bad pointer.  How did you find out the value of the string when you asked the question?
0
 
waysideCommented:
Are you seeing

{0x00325ff0 "0"}

when you hover the mouse over it in the debugger or you look at it in the QuickWatch window?

Or does the string actually contain a left brace, then a zero, then an x, then a zero, etc.?

What you show as the contents  ( m_gVal = {0x00325ff0 "0"}  ) exactly matches how the debugger displays a CString, so it is not clear to me exactly what the string displays.

This program:

#include "stdafx.h"
int main(int argc, char*argv[])
{
  CString foo("0");
  float f = atof(foo);
  printf("string foo is %f\n", f);

   return 0;
}

prints out

string foo is 0.000000

No casting or GetBuffer() is needed for using CString in atof(); since CString has a (LPCSTR) operator defined, and atof() takes a LPCSTR, the compiler will do the right thing.



0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now