Results 1 to 3 of 3

Thread: Newbie in C++ Programming needs help!

  1. #1

    Exclamation Newbie in C++ Programming needs help!

    Hi guys,

    Just picked up C++ learning. Need an advice. Would appreciate if anyone can help. Have searched the internet but not able to understand.

    The following conditions need to satisfy:

    1) To convert a string which may contains alpha-numberic to integer and ability to capture the Numberic in the string to store that value inside a integer.

    I came up with atoi and strtok() but an example would be greatly appreciated.. Thanks..


  2. #2
    Senior Member
    Join Date
    Jun 2003
    Location
    Singapore, Bedok
    Posts
    1,785

    Default Re: Newbie in C++ Programming needs help!

    From the help files:

    /* ATOF.C: This program shows how numbers stored
    * as strings can be converted to numeric values
    * using the atof, atoi, and atol functions.
    */

    #include <stdlib.h>
    #include <stdio.h>

    void main( void )
    {
    char *s; double x; int i; long l;

    s = " -2309.12E-15"; /* Test of atof */
    x = atof( s );
    printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

    s = "7.8912654773d210"; /* Test of atof */
    x = atof( s );
    printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

    s = " -9885 pigs"; /* Test of atoi */
    i = atoi( s );
    printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

    s = "98854 dollars"; /* Test of atol */
    l = atol( s );
    printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
    }

  3. #3

    Thumbs up Re: Newbie in C++ Programming needs help!

    Quote Originally Posted by ST1100 View Post
    From the help files:

    /* ATOF.C: This program shows how numbers stored
    * as strings can be converted to numeric values
    * using the atof, atoi, and atol functions.
    */

    #include <stdlib.h>
    #include <stdio.h>

    void main( void )
    {
    char *s; double x; int i; long l;

    s = " -2309.12E-15"; /* Test of atof */
    x = atof( s );
    printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

    s = "7.8912654773d210"; /* Test of atof */
    x = atof( s );
    printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

    s = " -9885 pigs"; /* Test of atoi */
    i = atoi( s );
    printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

    s = "98854 dollars"; /* Test of atol */
    l = atol( s );
    printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
    }
    Thanks.. will try...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •