C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 10th, 2002, 11:29 PM
jakobie jakobie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Chicago, IL
Posts: 5 jakobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 49 sec
Reputation Power: 0
Send a message via AIM to jakobie Send a message via Google Talk to jakobie
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

Reply With Quote
  #2  
Old October 11th, 2002, 01:26 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 1 m 11 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old October 11th, 2002, 08:00 PM
StealthElephant's Avatar
StealthElephant StealthElephant is offline
Shes dancing (obviously)
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2002
Location: the far side
Posts: 527 StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 38 m 39 sec
Reputation Power: 12
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?

Reply With Quote
  #4  
Old October 13th, 2002, 10:39 PM
jakobie jakobie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Chicago, IL
Posts: 5 jakobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 49 sec
Reputation Power: 0
Send a message via AIM to jakobie Send a message via Google Talk to jakobie
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.

Reply With Quote
  #5  
Old October 13th, 2002, 11:06 PM
StealthElephant's Avatar
StealthElephant StealthElephant is offline
Shes dancing (obviously)
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2002
Location: the far side
Posts: 527 StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 38 m 39 sec
Reputation Power: 12
mod 100

Reply With Quote
  #6  
Old October 14th, 2002, 11:49 AM
jakobie jakobie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Chicago, IL
Posts: 5 jakobie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 49 sec
Reputation Power: 0
Send a message via AIM to jakobie Send a message via Google Talk to jakobie
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.

Reply With Quote
  #7  
Old October 14th, 2002, 01:20 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
Quote:
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/

Reply With Quote
  #8  
Old October 14th, 2002, 01:47 PM
realnowhereman realnowhereman is offline
Not there when you need me
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2001
Location: Berlin, Germany
Posts: 1,430 realnowhereman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 46 sec
Reputation Power: 13
Quote:
Originally posted by jakobie
100 works but. If the year is 200X you get one number, not the two.

It's an int, so that's just a question of output formatting.
Code:
printf( "%d02", year % 100 );


HTH

Reply With Quote
  #9  
Old October 14th, 2002, 03:33 PM
StealthElephant's Avatar
StealthElephant StealthElephant is offline
Shes dancing (obviously)
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2002
Location: the far side
Posts: 527 StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level)StealthElephant User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 38 m 39 sec
Reputation Power: 12
if(( year % 100) < 10) cout << "0" << year % 100;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Splitting a Variable

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap