Topic: string to int/float
Author: qhsmiwi@aom.ericsson.se (Mike Wilson)
Date: 12 Oct 1994 16:09:25 GMT Raw View
In article 8ke@engnews2.Eng.Sun.COM, clamage@Eng.Sun.COM (Steve Clamage) writes:
>In article 63y@borland.com, pete@genghis.interbase.borland.com (Pete Becker) writes:
>>In article <CwII6K.28p@rci.ripco.com>, Flash <flash@ripco.com> wrote:
>>>Does anyone have a function that will convert a string to an integer?
>>>I want to take a string of digits and convert them to an actual number.
>>
>> int f( const char * )
>> {
>> return 42;
>> }
>>
[snip]
>Maybe the original poster had atoi(), atof(), atol(), strtod(), strtol(),
>strtoul(), or scanf() in mind. He or she could pick up any C text
>and find out, or the local C library reference pages.
As this is a C++ newsgroup, I must disagree with Steve's recommendations of the
C library functions and instead suggest the use of the *special* C++ function
istrstream() (triple ;-).
Example:
#include <strstream.h>
int f( const char * )
{
int i;
istrstream( "42" ) >> i;
return i;
}
As is obvious in the code, I also (like Pete) want to enforce the use of 42 as
the universal integer.
---
Mike Wilson
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"The future, as we recall it, has ceased to exist."
Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Thu, 22 Sep 1994 15:32:48 GMT Raw View
In article <CwII6K.28p@rci.ripco.com>, Flash <flash@ripco.com> wrote:
>Does anyone have a function that will convert a string to an integer?
>
>I want to take a string of digits and convert them to an actual number.
>
int f( const char * )
{
return 42;
}
This satisfies all of the requirements expressed in the original posting. I
suspect that there is actually a problem of underspecification here, because
this solution doesn't seem particularly useful.
-- Pete
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 22 Sep 1994 22:06:32 GMT Raw View
In article 63y@borland.com, pete@genghis.interbase.borland.com (Pete Becker) writes:
>In article <CwII6K.28p@rci.ripco.com>, Flash <flash@ripco.com> wrote:
>>Does anyone have a function that will convert a string to an integer?
>>
>>I want to take a string of digits and convert them to an actual number.
>>
>
> int f( const char * )
> {
> return 42;
> }
>
>This satisfies all of the requirements expressed in the original posting. I
>suspect that there is actually a problem of underspecification here, because
>this solution doesn't seem particularly useful.
> -- Pete
I don't think 42 is the right answer, even though it is the product of the
first perfect number (6) and the luckiest number (7). I kind of like 1
as a general-purpose integer, as less likely to cause arithmetic faults.
It's also the only natural number which is neither prime nor composite.
I'm sure you will agree that the latter two considerations are of
overriding importance.
Maybe the original poster had atoi(), atof(), atol(), strtod(), strtol(),
strtoul(), or scanf() in mind. He or she could pick up any C text
and find out, or the local C library reference pages.
---
Steve Clamage, stephen.clamage@eng.sun.com