1 Attachment(s)
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
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);
}
Got it (include stdlib.h)
Hello :)
Your solution worked as soon as I included the <stdlib.h>.
Thanks again!
Heather