I'm trying to take some numbers from a text file (eg. 34.54,23.43,45.98) and run some basic formulas on them. I'm reading the text file using Streamreader's Read() function which puts the data into a character array. My question is how do i get this data into a double format so that i can actually use it in the program?
Thanks.
char [] to double?
Started by Nabusman, Jul 17 2007 10:33 AM
7 replies to this topic
#1
Posted 17 July 2007 - 10:33 AM
|
|
|
#2
Posted 17 July 2007 - 12:31 PM
I'm not sure it is possible. How would you determine when the number you want starts and ends.
[0] = 3
[1] = 4
[2] = [.]
[3] = 5
[4] = 4
[5] = 2
[6] = 3
How would you know thats 34.54 and not 34.5423? Are all numbers only 4 digits [5 characters]? In that case it wouldn't be that difficult. [Dont know the exact syntax, but can help you with the theory]
[0] = 3
[1] = 4
[2] = [.]
[3] = 5
[4] = 4
[5] = 2
[6] = 3
How would you know thats 34.54 and not 34.5423? Are all numbers only 4 digits [5 characters]? In that case it wouldn't be that difficult. [Dont know the exact syntax, but can help you with the theory]
#3
Posted 17 July 2007 - 08:56 PM
Right, they are actually all numbers of 6 characters (one before the decimal, the decimal, and 4 numbers after). Plus, there is a comma delimiter, which i don't know if that helps. I was was thinking if it was possible to create a string and put up a for loop that loops through the array converting the char to a string and concatenating it to the string - if that makes any sense...
Of course then it would be possible to split the string using the comma as a delimiter. However, I'd have to use an array of 7 characters to include the comma.
Of course then it would be possible to split the string using the comma as a delimiter. However, I'd have to use an array of 7 characters to include the comma.
#4
Posted 17 July 2007 - 09:37 PM
Yes, that is exactly what I was thinking. If you have control over the text file you are using, it might be better to put each piece of data on its own line. I know a lot of programming languages have a 'readLine' function. That way you could 'get' strings rather than a character array.
But if you must, just create a nested loop. The outer looping through 7 elements, the inner looping through 6 elements. The inner loop will be your concatenated string which you can just cast to a double and use as you please.
I hope this makes sense.
But if you must, just create a nested loop. The outer looping through 7 elements, the inner looping through 6 elements. The inner loop will be your concatenated string which you can just cast to a double and use as you please.
I hope this makes sense.
#5
Posted 17 July 2007 - 10:45 PM
Makes perfect sense, don't know why i didn't think of that... I'll put each number on a different line. Thanks.
Nabs
Nabs
#6
Posted 18 July 2007 - 09:07 AM
Doing this is pretty easy.
Let's say you have an array: CharArray[7]
Let's say you have an array: CharArray[7]
int i;
float myDouble;
float myMult;
bool decimal;
myDouble := 0.0;
myMult := 1.0;
decimal := false;
for (i := 0 ; i++; i<=6)
{
if CharArray[i] = '.' then
{
decimal := true;
}
else
{
if !decimal then
myDouble := myDouble * 10
else
myMult := myMult / 10;
myDouble := myDouble + CharArray[i]*myMult;
}
};
#7
Posted 18 July 2007 - 09:48 PM
Thanks for the code WingedPanther, good to know to know how to do this.
Nabs
Nabs
#8
Posted 19 July 2007 - 08:16 AM
Looking at it, you'll have to add a conversion from the character to the digit, but that should be only a minor oversight.


Sign In
Create Account


Back to top









