Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-18-2004, 04:50 PM   #1
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
C++ function atof()

Im about to suicide after 6 hours of fumbling with this darn function; I can't get it to work the way I need.

I have a simple function which gets passed a string, uses insert and replace (with null characters) to get rid of part of the string, and is supposed to ouput a float which is simply atof of the string. I cannot, for the life of me, figure out how to get the string into the correct form for atof to use. I am certain that there aren't any invalid characters in it.

I know it has something to do with converting the string into a character array pointer, but I'm pretty new to C++ and can't figure it out.

Please help!!!!
__________________
Desktop
| Antec Performance Plus 1080AMG | Antec 430W PSU | Intel D875PBZ | Intel P4 3.0C |
| 4x512 MB dual-channel DDR400 Kingston HyperX | ATI Radeon 9800 pro 256 MB | Creative Sound Blaster Audigy Gamer |
| LiteOn DVD-ROM | LiteOn CDRW | 36 GB WD Raptor | 250 GB WD SATA2 | Sony Floppy | XP pro |
Iniamyen is offline   Reply With Quote
Old 06-18-2004, 05:35 PM   #2
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
atof(), atoi(), and family are deprecated. use strtod(), strtof(), strtol(), etc. depending on what libraries you are using, the atox() calls might even call strtox() for you.

either way, are you using cstring or character arrays? cstring lets you get the character string, something like cstring::c_str() or something, so that should not be a problem.

google "man strtod" or "man strtof" and it will give you the manual pages for string to double (float).

print out the string before you convert it for debugging. what is the literal string you are giving it?

check the return value afterwards. i believe it returns +/-INF (or maybe even NAN) for overflow and 0 for underflow. what value are you getting or is it segfaulting/crashing.

AS
AerynSedai is offline   Reply With Quote
Old 06-18-2004, 05:46 PM   #3
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
Something simple like this it won't let me do, the compiler says that it cannot convert type string into type const char*:

int main() {
string s = "4.25";
float f = strtof(s);
cout << f;
return 0;
}
Iniamyen is offline   Reply With Quote
Old 06-18-2004, 07:08 PM   #4
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
somewhere, depending on the compiler and library (C++ made a nightmare out of standardizing strings) there should be a member function called string::toCharArray() or something like that. in that case, use atof(s.toCharArray()) or strtof(s.toCharArray(),NULL).

there should be some function like this.
let me know if this works.

AS
AerynSedai is offline   Reply With Quote
Old 06-20-2004, 05:25 PM   #5
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
AerynSedai:

Never heard of the strto* family, but as far as I know, atoi and atof are still in the standard:
http://cppreference.com/stdstring_details.html#atof

Iniamyen:

You're getting an error message because atof returns double, here is how to do it:
Code:
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

int main() {
    string s = "4.25";
    float f = (float)atof(s.c_str());
    cout << f;
    return 0;
}

Last edited by aym; 06-20-2004 at 05:28 PM.
aym is offline   Reply With Quote
Old 06-20-2004, 07:49 PM   #6
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
well, many man pages i have read say ato* is deprecated. in fact, they often say the ato* family call the strto* with the proper standard parameters. i guess it depends what platform you are using.

i have been working on solaris boxes as of late. see if strto* exists on linux.

those functions are picky too. any problems and they crash. i try to use custom parsing.

but, yeah, all that needs to be done is use the c_str() member on the string.

AS

Last edited by AerynSedai; 06-20-2004 at 07:53 PM.
AerynSedai is offline   Reply With Quote
Old 06-21-2004, 11:09 AM   #7
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
The c_str() function seems to work fine. I am having the darndest time learning C++ (well, the C really, I've done some OOP before with Java.) Thanks much for the tip!!
Iniamyen is offline   Reply With Quote
Old 06-26-2004, 05:35 PM   #8
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Quote:
Originally posted by AerynSedai
well, many man pages i have read say ato* is deprecated. in fact, they often say the ato* family call the strto* with the proper standard parameters. i guess it depends what platform you are using.

i have been working on solaris boxes as of late. see if strto* exists on linux.

those functions are picky too. any problems and they crash. i try to use custom parsing.

but, yeah, all that needs to be done is use the c_str() member on the string.

AS
I read the man pages for strto*, looks like they are in the standard too.
aym is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:47 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2