Thread: converting Ansi String to double or constant char* value (strtod?)

  1. #1
    Registered User
    Join Date
    Dec 2001

    converting Ansi String to double or constant char* value (strtod?)

    I need to know how to convert an 'Ansi String' to a 'constant char*' or even better how to convert an 'Ansi String' to a double (value).

    I've tried:
    char *end=".";
    String NewValue = OrigValue->Text;
    constant = strtod(NewValue,&end);

    However, I get the error
    "can not convert 'Ansi String' to 'constant char*'"

    I'm using C++ Builder and I have a TEdit box that I read Text values from, which I want to covert to numeric values.

    Would apprciate any help.

    Thanks in advance.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    I'm not sure about this but I think you can use the c_str function to convert it to a char pointer (or array):
    strcpy(buf, OrigValue->Text.c_str());

  3. #3
    Registered User
    Join Date
    Dec 2001
    Thank you Monster.
    I think you must be on the right track.
    I don't get a compile error with that function.
    However, I get a runtime error with exceptio class C0000005 w/message 'access violation at 0x00403065.

    What could this mean?

  4. #4
    Registered User
    Join Date
    Nov 2001
    Give an example with real data. For example, what kind of data is in the string object? Are you use a string object or an array?

    One solution for convert raw data from a string object to a double data type is atof().

    double nValue = atof(&stringObject[0]);

  5. #5
    Registered User
    Join Date
    Mar 2002
    see if AnsiString has direct conversion function. I believe it has one for ints:
    AnsiStirng string;
    int num = string.ToInt();

    if not use the c_str() in combination with atof()

    AnsiString string;
    cout << "enter a floating point value" << endl;
    cin >> string;
    couble num = atof(string.c_str());

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Yes, indeed use c_str() of AnsiString to get a null terminated array.


    AnsiString::ToDouble to get floating point value from string contents.

    Also note that AnsiString has char* constructor & assignment operator, therefore it is valid to do this.

    char* cstring[100];
    strcpy(cstring, "hello world");
    AnsiString as = cstring;

    Which is, incidently, equivelant to simply:

    AnsiString as = "hello world";

    In general, see the AnsiString page in the Borland help file, all the methods are listed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM