C++: String to Integer cast

This is a discussion on C++: String to Integer cast within the C++ Programming forums, part of the General Programming Boards category; I have a string, taken via a substring. I need to convert it into an integer. The string is of ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    37

    Red face C++: String to Integer cast

    I have a string, taken via a substring. I need to convert it into an integer. The string is of class string. I.e.:

    Code:
    string line;
    // line inputs from a file...
    int index= line.find(' ',0);
    string temp= line.subt(0, index);
    I know that temp is a number...because the file I'm inputting from has the following line (I checked the value of line, it's correctly inputted):

    1 -1 -1 1.

    How can I convert it?

    --Ashiq

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Code:
    #include <cstdlib>
    ...
    int i = atoi(temp.c_str());
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    37
    Whoa, thanks. Simple solution. My copyover into a char array was really messed anyway...peace!

    --Ashiq

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Does any bother to look through THE FAQ nodays???
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21