Thread: Converting string to int.

  1. #1
    mosdef
    Guest

    Converting string to int.

    I wish to convert a string to an int. The string may contain digits (0-9), a negative sign (-), or a decimal point (.). Is there a function to handle this? Can you be quickly written? Btw, I can either use c-style char * strings, or STL strings.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    atoi()
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    atoi() for null terminated strings.

    toInt() or some such function for STL strings.

  4. #4
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Ever tried type casting??
    Code:
    char my_string [1];
    cout << "my_string: " << my_string;
    cout << "int(my_string): " << int(my_string);
    I think that's it
    Good Luck
    what does signature stand for?

  5. #5
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282
    Code:
    char my_string [1];
    initialize it dude....

    char mychar = 'a';

    int (mychar) works... strange... but most
    books say (int)mychar for typecasting...

  6. #6
    mosdef
    Guest
    Thanks guys... so what you suggested will handle non digit characters such as the negative sign and decimal point, right?

  7. #7
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    atoi

    i think atoi() is better than typecasting the thing.
    so use atoi()

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i think atoi() is better than typecasting the thing.
    >so use atoi()

    I agree with moonwalker.

  9. #9
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Originally posted by moonwalker
    Code:
    char my_string [1];
    initialize it dude....

    char mychar = 'a';

    int (mychar) works... strange... but most
    books say (int)mychar for typecasting...
    Ok Whatever you say
    what does signature stand for?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >atoi() for null terminated strings.
    atoi is notorious for causing problems and most experienced programmers place it with gets to be removed from the language at the first chance. The problem is in how it handles errors, it's far better to use strtol and cast the result to int. Another option is to use sscanf or stringstreams to read an integer value from a C-string or C++ string to an int variable.

    -Prelude
    My best code is written with the delete key.

  11. #11
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Hey, can you make type casting in C ??
    what does signature stand for?

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    int one;

    double two = 2.0;

    one = (int)two;

    c style type casting. Look up dynamic_cast<>, static_cast<>, etc. for c++ style casting.

  13. #13
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Ruski
    Hey, can you make type casting in C ??
    oh sure, it will let you, in fact C will let you write just about anything you want ("who cares what it means? i just compile it", said the compiler)

    but this:

    Code:
    #include <stdio.h>
    
    int main (void)
    {
      char mystring[] = "46";
      int i;
      i = (int) mystring;
      printf ("%i\n", i);
      return 0;
    }
    means something totally different than what you're looking for. (and no, the program doesn't print out 46 when run)
    hello, internet!

  14. #14
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Thanks for the reply
    what does signature stand for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM