DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Conver string to int

  1. #1
    Join Date
    Apr 2011
    Posts
    13

    Conver string to int

    Hello everyone

    Could anyone please tell why does the following code not convert the string into int? When I pass a hard coded string e-g "123456" then the code below works, but when I pass a string element from a string type vector, the code doesn't work. Or is there any other simple way of conversion?
    Code:
    class.h
    class cls
    {
    	public:
    	template <class T> bool from_string(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&)); 
    };
    class.cpp
    Code:
    template <class T> bool cls::from_string(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&))
    {
    	std::istringstream iss(s);
    	return !(iss >> f >> t).fail();
    }
    
    void cls::somefunct
    {
    	int i;
    	string strn = vector[1];
    	if(from_string(i, std::string(strn), std::dec))
    	{
    		std::cout << "i: " << i << std::endl;
    	}
    	else
    	{
    		std::cout << "from_string failed" << std::endl;
    	}
    }

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    This simple task has been overcomplicated by changes in the language. Time was you just ran atoi(...) and it was done, --- these days you need to store the same information in 3 containers using 10 lines of code and 50 times the CPU effort to get the same result.


    Typically such problems are done in modern c++ by having you storing the numeric value in an integer, the string version in a string, and converting between the two with a stringstream.

    A short example of this is:

    int main()
    {
    string s = "1234";
    stringstream ss(s);
    int i;
    ss >> i;
    cout << i << endl;

    return 0;
    }

    Because the c++ tools are so aggravating, boost also provides a library that can do this for you, but I do not remember which tools exactly you need to get to use it.
    Last edited by jonnin; 05-06-2011 at 12:12 PM.

  3. #3
    Join Date
    Dec 2007
    Posts
    401
    The atoi() family of functions will not do the job at all here, where there is a need is (there usually is) to detect errors in conversion.

    With the strtol() family of functions, error detection is possible; however you would need to use different functions for different types. Also, these are available only for a few standard types.

    There is nothing wrong with the function you have written. Though if you want to ensure that the entire string has been fully consumed, you need to write it this way:

    Code:
    template< typename T, typename MANIP >
    inline bool from_string( T& t, const std::string& s, const MANIP& m )
    {
    	std::istringstream iss(s);
    	return iss >> m >> t && iss.eof() ;
    }
    Verify that the vector contains what you expect it to contain; the problem seems to be there.

  4. #4
    Join Date
    Apr 2011
    Posts
    13
    Thanks Jonnin and Vijayan for your help :)

Similar Threads

  1. Replies: 7
    Last Post: 03-17-2009, 12:40 AM
  2. No
    By chintucs in forum Java
    Replies: 2
    Last Post: 01-21-2007, 05:08 PM
  3. KeyListener not responding
    By lordanki in forum Java
    Replies: 3
    Last Post: 04-04-2006, 10:13 AM
  4. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM
  5. Replies: 0
    Last Post: 06-22-2000, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center