CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Posts
    3

    Type conversion (item in char array, to a double)

    Hello

    I'm actually writing an assignment in C for school. I am a VBA programmer so bare with me.

    I have been trying to assign an item (numerical value) in a character array to a variable (with the data type of double).

    For example, I use a scanf() to get the input "hahidi9999" or to capture things like "9999". If it is is numeric, such as 9999. I want to take the value 9999 which is stored in a character array test[0], assign it to another variable usDollars (a double), so I can then perform a mathematical calculation.

    In VB/VBA this is very easy to do with conversion functions. However, I am not as familiar with C and am having trouble finding such functions.

    I have attached a sample I've been working with (.c extension). It is driving me crazy....the compiler we are working with is a free one called Miracle C.

    Any help would be great.
    Thanks
    Heather
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    Try atof():

    Code:
    double Num=atof(Test[0]);
    This assumes that Test is an array of strings (ex. char *Test[10], char **Test or similar).

    TDM

  3. #3
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    ....and after looking at the code I see that you have used char Test[25]. So.....

    Code:
    void ConvertTest(char *input)
    {
        double usDollar=0.0;
        double test=atof(input);
        printf("test = %f\n",test);
        printf("input = %s\n", input);
        printf("US Dollar Amount entered %.2f\n",test);
    }
    .....or something similar.

    TDM

  4. #4
    Join Date
    May 2004
    Posts
    3

    Thanks - still weird results though

    Hi

    Thank you sooooo much. I was going crazy looking for a function like this.

    However, the output is still not correct. Here is the revised function.

    So if the character string input is 11111, then usDollar should display 1111, but it will show another number instead.
    Any idea why?

    Thanks so much!
    Heather

    void ConvertTest(char *input)
    {
    double usDollar=0.0;
    usDollar=atof(input);
    printf("input = %s\n", input);
    printf("US Dollar Amount entered %.2f\n",usDollar);
    }

  5. #5
    Join Date
    May 2004
    Posts
    3

    Got it (include stdlib.h)

    Hello

    Your solution worked as soon as I included the <stdlib.h>.

    Thanks again!
    Heather

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured