Thread: string to int

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    string to int

    Hello..

    Whats the best/right way to convert c++ string to int?

    Thanks for help

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Read the FAQ.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    How can you convert a string to int anyway?
    Do you want the int to contain the length of the string or the ascii character code of the first character? Or something else?
    I don't really understand your question...

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    string test = "432";
    int i;

    now i want to convert test to i = 432;

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    string test="432";
    int i=atoi(test.c_str());

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by maxorator
    Code:
    string test="432";
    int i=atoi(test.c_str());
    What about using sstream? Isnt that option better?

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. Because the way atoi works is... strange... to be nice to it and more often than not forces code to be written in order to tame it.

    atoi("12 - Hello Mum!") will gladly be converted to 12.
    and atoi("What a wonderful strange thing to return") will gladly return 0.

    http://faq.cprogramming.com/cgi-bin/...wer=1046996179 for more control on the conversion.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Hey..

    Heres my version of string convertion:

    Code:
    template <class t>
    t string_convert(const string &s) {
    	istringstream stream(s);
    	t i;
    
    	if (stream >> i)
    		return i;
    
    	return 0;
    }
    I call it with
    int i = string_convert<int>(some_string);

    But I get link error.. No idea why..

  9. #9

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    This can do string to int, or int to string or whatever - http://cboard.cprogramming.com/showp...32&postcount=7

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by twomers
    This can do string to int, or int to string or whatever - http://cboard.cprogramming.com/showp...32&postcount=7
    Could you give an example how to use it?

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    std::string s("1234321");
    int i = jsw::lexical_cast(s);
    Read the FAQ, dammit.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Tonto
    Code:
    std::string s("1234321");
    int i = jsw::lexical_cast(s);
    Read the FAQ, dammit.
    Thanks.. One more thing, what would be the right way to catch exception? I've never been doing that before. Is this a smart way to do it?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > One more thing, what would be the right way to catch exception?
    Baseball gloves - exceptions travel at a heck of a speed, so you need something with padding
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by l2u
    Hey..

    Heres my version of string convertion:

    Code:
    template <class t>
    t string_convert(const string &s) {
    	istringstream stream(s);
    	t i;
    
    	if (stream >> i)
    		return i;
    
    	return 0;
    }
    I call it with
    int i = string_convert<int>(some_string);

    But I get link error.. No idea why..
    There is no way that's going to work because the compiler doesn't know what type to return. Try passing in a argument to your template func.

    Code:
    template <class T>
    void string_convert(const std::string &str, T &myVar)
    {
        .. code here
        myVar  = str converted to T
    }
    EDIT: nvm, I would use the stringstream option if i had to do this
    EDIT2: I may and probably am wrong about the compiler not knowing what type to return (that is specified by T), however I can say that returning 0 in ALL cases is probably not appropriate.
    EDIT3: Bah, I had to get this working here's the code I came up with:

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    template <class T>
    T string_convert(const std::string &s) {
    	std::istringstream stream(s);
    	T i;
    
    	if (stream >> i)
    		return i;
    
    	return 0;
    }
    
    int main()
    {
        std::string myStr("12345");
        
        std::cout << string_convert<int>(myStr);
        return 0;
    }
    Last edited by MacNilly; 09-10-2006 at 12:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM