I am trying to write a function that reads numbers out of a string of comma separated values. The first function is:
unsigned get_frame(unsigned &index, char input[])
{
const comma=',';
char *letter;
char *accum="";
unsigned out;
while (input[index]!=comma)
{
*letter=input[index];
strcat(accum,letter);
index++;
};
out=atoi(accum);
index++;
return(out);
};
This function reads the string "input" until it finds a comma, then returns the value of the numbers it just read as a unsigned integer. This works fine. However, when I repeat the function to read the next number as a float, the problems occur. Here is the next function:
float get_matrix(unsigned &index, char input[])
{
const comma=',';
char *letter;
char *accum="";
float out;
while (input[index]!=comma)
{
*letter=input[index];
strcat(accum,letter);
index++;
};
out=atof(accum);
index++;
return(out);
};
As you can see, it's the same function, this time using atof and float. But, the second function does not work. The string accumulator "accum" starts with a long string of nonsense characters that do not go away.
I don't know where they come from, or how to deal with them. Why does this function work the first time and not the second?
Any help will be appreciated.