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

This is a discussion on converting Ansi String to double or constant char* value (strtod?) within the C++ Programming forums, part of the General Programming Boards category; I need to know how to convert an 'Ansi String' to a 'constant char*' or even better how to convert ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    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
    Posts
    680
    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):
    Code:
    strcpy(buf, OrigValue->Text.c_str());

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    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
    Posts
    1,348
    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().

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

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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
    Posts
    812
    Yes, indeed use c_str() of AnsiString to get a null terminated array.

    Also

    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, 03:45 PM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 03:56 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21