how to convert a string to number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • palani12kumar
    New Member
    • Oct 2006
    • 60

    how to convert a string to number

    hi. I want to get a string as input.
    If the input is an integer i want to save it to an int variable.
    I know to convert a number to int. but how to convert a string to number(integer) . in C++
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by palani12kumar
    hi. I want to get a string as input.
    If the input is an integer i want to save it to an int variable.
    I know to convert a number to int. but how to convert a string to number(integer) . in C++
    In C++ you can use stringstream for this, see
    http://www.cppreferenc e.com/cppsstream/all.html

    Comment

    • wiss
      New Member
      • Mar 2007
      • 1

      #3
      Originally posted by palani12kumar
      hi. I want to get a string as input.
      If the input is an integer i want to save it to an int variable.
      I know to convert a number to int. but how to convert a string to number(integer) . in C++
      you can apply atoi () or atof () from the c language.

      string s = "10";
      int i = atoi(s.c_str()) ;

      Comment

      • palani12kumar
        New Member
        • Oct 2006
        • 60

        #4
        Thank you for the kind Help

        Comment

        Working...