CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2009
    Posts
    19

    how to convert a string to integer with C++

    Does anyone know how I could convert a string into int in C++?

    I have the code ready set up but the result isn't what I expect.

    example:

    year = date_input.substr(second_slash, 4);

    stringstream ss(year);
    int in_year;
    ss>> in_year;


    let me know.. thanks!!

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: how to convert a string to integer with C++

    int in_year = atoi(ss.c_str());
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: how to convert a string to integer with C++

    Without questioning Superman’s suggestion, I’d like to know what didn’t work in your code.
    This fragment:
    Code:
    	std::string year("1234");
    	std::stringstream ss(year);
    	int in_year;
    	ss>> in_year;
    produces correct result.
    What did your call
    Code:
    year = date_input.substr(second_slash, 4);
    returned? What was date_input? And what was second_slash?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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

Featured