Results 1 to 6 of 6

Thread: convert strings

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post convert strings

    anyone know how to convert strings into integers?

    strings I meant not chars, I meants strings.

    #include <string.h>

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    simple enough

    PHP Code:
    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
        
    string s "1234";
        
    int    i atoi(s.c_str());

        
    cout << << endl;

        return 
    0;

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    just for future knowledge the opposite is itoa

  4. #4
    jim mcnamara
    Guest
    Use the atoi() function.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    _itoa is a non-standard extension of MS to the CRT. The C standard would be sprintf.

    For C++, the standard method would be a stringstream:
    Code:
    #include <sstream>
    #include <string> // no .h, this is old standard and deprecated
    #include <iostream>
    using namespace std;
    
    int main()
    {
      // int to string:
      ostringstream os;
      os << 1249 << " " << 2.34e12;
      string s = os.str();
      cout << s << endl;
      // string to int
      istringstream is(s);
      int i;
      double d;
      is >> i >> d;
      cout << i << " " << d << endl;
    return 0;
    }
    Does this make you happy parksie?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yep

    string.h is deprecated in C++ in favour of <cstring>, which is identical apart from placing everything inside namespace std
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width