C++ strtok

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eboyjr14
    New Member
    • Apr 2007
    • 27

    C++ strtok

    Let's say that:

    argv[i] = "Db4+2";

    How can I get the number 2 from that as an integer?

    In PHP it would be easy:

    [PHP]
    <?php
    $argv[$i] = "Db4+2";

    $argvarr = explode("+", $argv[$i]);
    echo $argvarr[1];
    // Would return: 2

    ?>
    [/PHP]

    I know PHP fluently, and I am just learning C++.
  • eboyjr14
    New Member
    • Apr 2007
    • 27

    #2
    Remeber there may not be a +2 or whatever, it could just be Db4

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Maybe something like:
      [code=cpp]
      int length = strlen(argv[i]);
      int value = 0;
      for (int ctr = 0; ctr < length; ++ctr)
      {
      if (argv[i][ctr] == '2')
      {
      value = argv[i][ctr] - 48;
      }
      }
      cout << value << endl;
      [/code]

      This is just a loop that searchs for the character 2 and if found changes it to an integer 2 by subtracting the offset from '2' to 2 in the ASCII table.

      Comment

      • eboyjr14
        New Member
        • Apr 2007
        • 27

        #4
        Originally posted by weaknessforcats
        Maybe something like:
        [code=cpp]
        int length = strlen(argv[i]);
        int value = 0;
        for (int ctr = 0; ctr < length; ++ctr)
        {
        if (argv[i][ctr] == '2')
        {
        value = argv[i][ctr] - 48;
        }
        }
        cout << value << endl;
        [/code]

        This is just a loop that searchs for the character 2 and if found changes it to an integer 2 by subtracting the offset from '2' to 2 in the ASCII table.

        but... it could be any number, not 2. But... I already figured it out. I would show you the source but its on my iPod and I don't have it with me. Thx, anyway!

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by eboyjr14
          but... it could be any number, not 2. But... I already figured it out.
          I presume you know how to use the ASCII table. Yes?

          Comment

          Working...