1,105,578 Community Members

converting the date to an int

Member Avatar
stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi everyone,
I am trying to write a program for a class but I cannot seem to figure out how I can convert a date in the format mm/dd/yyyy to separate int values. I.E. m+m+d+d+y+y+y+y. I've got the rest of the program written, but I cannot seem to figure this little part out. The part I am having trouble with is separating the slashes out of there. Thanks in advance!
Nick

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

Is this C or C++? How is the date originally stored? I assume it's a string, but you need to be more specific about what you need or we'll be of no help to you.

Member Avatar
stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry, it is in c++ and the date is originally stored as a string. I think I may be on the right track... I think by using the <cctype> library, i can distinguish the numbers from the slashes, but i need to be able to then convert it into integers E.G. the month to an integer, the day to an integer and the year to an integer. Would this be in the <iomanip> library? Thank you for your help.
Nick

Member Avatar
Ancient Dragon
Achieved Level 70
27,637 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

if you know the format, then its pretty easy

string date = "01/02/2005"; // 1 Feb 2005
int month = atoi(date.substr(3,2).c_str());
// do day and year similar to above
Member Avatar
stupidenator
Junior Poster
192 posts since Mar 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the help! I finally got it working. I was not aware earlier that you could just do somehting like

cin >> int >> char >> int >> char >> int;

Thank you to everyone who tried to help me
Nick

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article