# strtok and StrTo Float problems

First post - I program for fun only. Sorry if not a perfect question. I'll give you all 75 points for solution that works.

Short question:
How can I parse a char* twice?

Here's a better description:
When reading a comma separated text file which contains a decimal value (such as 456.12345), the StrToFloat function returns the "incorrect" value (it is not exact). I know this is is because of the rounding (althought not really rounding) issue with StrToFloat and atof.

My solution (that I can't get to work) is to parse the number string into two parts, the integer and the decimal. Then convert the integer portion to an integer. Take the decimal, truncate it at say 6 characters and convert it to a float (rounding it to 1 if necessary). Divide it by 1000000 and add the int to it.

The cause of the problem (I think) is nesting strtok functions.

I get the numberstring by parsing on a "," I want to take that char* and parse it again into two other strings.

Full code available upon request.

Thanks in advance.
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
hi there. as i understand u want the 12345 as one string and the 456 as another one. solution is, write two FORs to once parse from rite to left and once from left to rite, using substrings. it would roughly look sth like this:

string a = 456.12345
string b = rightside
string c = leftside

parse from right to left:
for(int i=a.length(); i>0; i--;){
if(a.substr(a, a-1)=='.'){b=a.substr(a, a-1); break;}
}

parse from left to rite:
for(int i=0; i<a.length(); i++;){
if(a.substr(a, a+1)=='.'){c=a.substr(a, a+1); break;}
}

this will give u b=12345 and c=456

this is the basic idea behind it. u will have to look what exactly the syntax for substrings and length of strings are, but it can easily be found in the c++api.
0
Commented:
heck sorry, my first reply^^ the functions have to look like that, coz i is the variable in the for-thingys, not a:

parse from right to left:
for(int i=a.length(); i>0; i--;){
if(a.substr(i-1, i)=='.'){b=a.substr(i, a.length()); break;}
}

as of a.length() is the last pos of the string.

parse from left to rite:
for(int i=0; i<a.length(); i++;){
if(a.substr(i, i+1)=='.'){c=a.substr(0, i); break;}
}

as of 0 is the first pos of the string.
0
Commented:
You can read a comma separated file containing float values as follows,

char buffer[250];

float f[5];
int count = 0;

ifstream in;
in.open( "test.txt", ios::in );
in.getline( buffer, 249, '\n' );
in.close();

char* token = strtok( buffer, "," );

while( token )
{
f[count] = atof( token );
token = strtok( 0, "," );
count++;
}

cout << endl;

for( int x = 0; x < 5; x++ )
cout << f[x] << endl;

Exceter
0
Commented:
Of course, you can forget about strtok entirely by saying something like,

float f[5];
char c;

in >> f[0] >> c >> f[1] >> c >>f[2] >> c >> f[3] >> c >> f[4];

By the way, here is a sample textfile that the code I have posted will extract floats from.

-- test.txt --

123.5,234.56,1234.6778,56.3,234.5678

-- end of file --

Exceter
0
Author Commented:
Thanks for the answers. I will look into both methods described here. Unfortunately I won't get a chance to do it for a few days. I just didn't want you to think I didn't want to accept an answer.
0
Commented:
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.

In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Udil
EE Cleanup Volunteer
0
Commented:
From reading this quesiton, it appears I missunderstood what the asker wanted when I intially posted and, therefore, the points should go to stoliD.

Exceter
0
Commented:
yup thx, i could need some credits.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Sorry I didn't get back sooner. I had to stop with that program for a while, and didn't know who gave the best answer. Exceter helped me decide. Thanks to both.
0
Commented:
You are most welcome.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.