?
Solved

strtok and StrTo Float problems

Posted on 2003-02-18
10
Medium Priority
?
419 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:mralarming
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:stoliD
ID: 7978502
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
 
LVL 1

Expert Comment

by:stoliD
ID: 7978540
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
 
LVL 8

Expert Comment

by:Exceter
ID: 7978813
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:Exceter
ID: 7978878
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 Comment

by:mralarming
ID: 7984668
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
 
LVL 2

Expert Comment

by:udil
ID: 8867139
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8871964
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
 
LVL 1

Accepted Solution

by:
stoliD earned 300 total points
ID: 8884181
yup thx, i could need some credits.
0
 

Author Comment

by:mralarming
ID: 8890348
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8898403
You are most welcome.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question