Thread: converting string to int

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    converting string to int

    Hello!
    How to convert string to int?
    Is there any standart function for this.
    atoi() doesn't work. one way is to convert string to char array and then atoi().
    is there something short like
    int x=str.toint();

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are quite a few threads about this topic....

    strtol()
    atoi().

    Do a board search to find examples.


    [edit]
    If your talking about a c++ string object, don't forget about this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    string str("234");
    int var=atoi(str);

    gives compiler error because it can handle only
    char arrays;

    I find that stringstream is useful.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    gives compiler error because it can handle only
    char arrays;
    If your talking about a c++ string object, don't forget about this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    string whatever="12345678";
    atoi(whatever.c_str());

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