|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
|
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 | |
|
|
|
|
|
#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 |
|
|
|
|
|
#3 |
|
Member (10 bit)
|
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; } |
|
|
|
|
|
#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 |
|
|
|
|
|
#5 |
|
Registered User
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. |
|
|
|
|
|
#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. |
|
|
|
|
|
#7 |
|
Member (10 bit)
|
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!!
|
|
|
|
|
|
#8 | |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|