Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

strtok and StrTo Float problems

Posted on 2003-02-18
10
Medium Priority
?
429 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

580 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