#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Chicago, IL
    Posts
    5
    Rep Power
    0

    Splitting a Variable


    I know this is going to end up turning into something really simple but...

    I have a variable with the year 2002. I need to print it as both 2002 and 02. How can I only print the last two digits of a variable?


    Thank you
    Jake
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Assuming you have the variable in a char array, you can always offset or increment a char pointer by 2 positions something like this:
    Code:
    char *date = "2002";
    
    printf("Long date is %s\n", date); /* Prints 2002 */
    
    /* Offsetting the pointer by 2 positions. *date still points to 2002 */
    printf("Last two digits are %s\n", date+2); /* Prints 02. */ 
    
    /* Increment char pointer by 2 characters */
    date += 2; /* Now *date points to "02" */
    printf("Last two digits are %s\n", date); /* Prints 02 */
    Hope this helps :)
  4. #3
  5. Shes dancing (obviously)
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2002
    Location
    the far side
    Posts
    527
    Rep Power
    14
    well if its not in a character array, you could always just mod the number by 1000 :)

    microsofts butterfly is their way off telling u their systems have a **** load of buggs
    Advocating Linux Guide
    Lesbian Linux
    Great & Practical Computer Books

    like the links?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Chicago, IL
    Posts
    5
    Rep Power
    0
    The whole Modulus thing doesn't work if 1999 is put in, you get 999. If you put in 2002 you get 2. How would I offset in C++. I tried strncpy but I got an error. I was trying to do like,
    Code:
    strncpy(two, four, 2)
    thats not the same one, there was a different one where before the amount of characters you could specify on which character to start coping on.
  8. #5
  9. Shes dancing (obviously)
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2002
    Location
    the far side
    Posts
    527
    Rep Power
    14
    mod 100

    microsofts butterfly is their way off telling u their systems have a **** load of buggs
    Advocating Linux Guide
    Lesbian Linux
    Great & Practical Computer Books

    like the links?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Chicago, IL
    Posts
    5
    Rep Power
    0
    I'm thinking the Mod method would be too big of a pain. 100 works but. If the year is 200X you get one number, not the two.

    I tried incrementing a char pointer, and well, I got 52 and 51 for 2005 and 1999.

    Thanks guys.
  12. #7
  13. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    I tried incrementing a char pointer, and well, I got 52 and 51 for 2005 and 1999.
    That's odd. Are you sure you're increasing the pointer, and not something else?

    Anyway, here's an example program that generates a random year between 1900 and 2100, and prints it out both long and short:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int
    main (int argc, char **argv)
    {
            int year;
            char s[5];
    
            srand(time(NULL));
    
            year = 1900 + rand() % 200;
    
            /* convert integer to string */
            snprintf(s, sizeof(s), "%4d", year);
    
            printf("%s\n", s);
    
            /* skip past the first two digits */
            printf("%s\n", s + 2);
    }
    Hope this helps.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  14. #8
  15. No Profile Picture
    Not there when you need me
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Oct 2001
    Location
    Berlin, Germany
    Posts
    1,430
    Rep Power
    14
  16. #9
  17. Shes dancing (obviously)
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2002
    Location
    the far side
    Posts
    527
    Rep Power
    14
    if(( year % 100) < 10) cout << "0" << year % 100;

    microsofts butterfly is their way off telling u their systems have a **** load of buggs
    Advocating Linux Guide
    Lesbian Linux
    Great & Practical Computer Books

    like the links?

IMN logo majestic logo threadwatch logo seochat tools logo