Remove first two characters from char* or from int?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yogi_bear_79

    Remove first two characters from char* or from int?

    I have a char *year that contains a 4-digit year (i.e 1929), I use the
    following syntax c = atoi(year); to convert it to an integer. However
    I must strip the first two numbers. I've tried to convert it from
    char* to a string and use the erase function, which works, but then I
    can't seem to convert the string it to an integer. I am impartial as
    to when I remove the first two digits, meaning it can be done before
    or after the conversion to int. In my example above the end result
    should be an integer variable equalling 29.

  • Jerry Coffin

    #2
    Re: Remove first two characters from char* or from int?

    In article <58fb4e42-cbe5-48ad-bea6-4149eb5237a1
    @p25g2000hsf.go oglegroups.com> , yogi_bear_79@ya hoo.com says...
    I have a char *year that contains a 4-digit year (i.e 1929), I use the
    following syntax c = atoi(year); to convert it to an integer. However
    I must strip the first two numbers. I've tried to convert it from
    char* to a string and use the erase function, which works, but then I
    can't seem to convert the string it to an integer. I am impartial as
    to when I remove the first two digits, meaning it can be done before
    or after the conversion to int. In my example above the end result
    should be an integer variable equalling 29.
    c = atoi(year+2);

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.

    Comment

    • yogi_bear_79

      #3
      Re: Remove first two characters from char* or from int?

      On Mar 5, 10:01 pm, Jerry Coffin <jcof...@taeus. comwrote:
      In article <58fb4e42-cbe5-48ad-bea6-4149eb5237a1
      @p25g2000hsf.go oglegroups.com> , yogi_bear...@ya hoo.com says...
      >
      I have a char *year that contains a 4-digit year (i.e 1929), I use the
      following syntax c = atoi(year); to convert it to an integer.  However
      I must strip the first two numbers.  I've tried to convert it from
      char* to a string and use the erase function, which works, but then I
      can't seem to convert the string it to an integer.  I am impartial as
      to when I remove the first two digits, meaning it can be done before
      or after the conversion to int.  In my example above the end result
      should be an integer variable equalling 29.
      >
      c = atoi(year+2);
      >
      --
          Later,
          Jerry.
      >
      The universe is a figment of its own imagination.
      Perfect! Thanks!

      Comment

      • Juha Nieminen

        #4
        Re: Remove first two characters from char* or from int?

        yogi_bear_79 wrote:
        >c = atoi(year+2);
        Perfect! Thanks!
        Or alternatively: c = atoi(year) % 100;

        (This will work even if there's whitespace at the beginning of the
        string, or the year is already 2-digit in the string.)

        Comment

        Working...