How do I convert string type to int?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bushnellweb
    New Member
    • Jan 2010
    • 48

    How do I convert string type to int?

    The answers i found sucked so maybe someone can shed some new light on the situation.

    I have a 2-dimensional array of string type

    string string1[2][4];

    string1[0][0] = integer
    string1[0][1] = string
    string1[0][2] = float
    string1[0][3] = integer

    i need to read all of it into 1 two-dimensional array but when its a string type how do i get the integers to be an integer???
  • bushnellweb
    New Member
    • Jan 2010
    • 48

    #2
    i guess i figured it out but every other answer i saw was soo complicated for some reason. all i did was

    float floatNumber = atof(string1[0][2].c_str());

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      If you are using string then you are using C++. The C++ way to do this would be a stringstream.

      Code:
      float floatNumber;
      stringstream sstream(string1[0][2]);
      stream >> floatNumber;

      Comment

      Working...