Convert string to int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankaj255143
    New Member
    • Oct 2007
    • 17

    Convert string to int

    i just joined this community. i have a problem - how can i convert a string(which contains numaric values) into a integer variable.
    for eg. -
    char date[10];
    here date[0,1] contains date
    and date[3,4] contains the value of month.
    i want to stor the date and maonth in integer variables.dd and mm. then how can i do it....
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by pankaj255143
    i just joined this community. i have a problem - how can i convert a string(which contains numaric values) into a integer variable.
    for eg. -
    char date[10];
    here date[0,1] contains date
    and date[3,4] contains the value of month.
    i want to stor the date and maonth in integer variables.dd and mm. then how can i do it....
    Assiging a char value to an integer variable will convert the data to integer internally. i mena you need not do any external conversion.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      I would recommend looking into the atoi() function.

      Because your date is in a single string, you will have to parse it out. However, that post was just to answer your "string to int" question. I would recommend doing something different entirely and using a date class. You can use a format to give your class the same format your string is in, and then create a date object passing that string to the constructor. Then you'll be able to call the day, month, year, whatever, individually.

      PS - you would have been able to find the function more quickly if you had done a Google search of "c (or c++) string to int".

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        atoi() is OK in C but is deprecated in C++.

        Are you using C++?

        Comment

        Working...