Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,536 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,522 people online right now. Registration is fast and FREE... Join Now!




strings

 
Reply to this topicStart new topic

strings

Latina
20 Nov, 2007 - 10:12 PM
Post #1

New D.I.C Head
*

Joined: 11 Nov, 2007
Posts: 2


My Contributions
If I am storing the date the user enters how I can know what the month day and year is.
Ex: November 20, 2007
Can some one help me to code that?
Thanks

User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Strings
20 Nov, 2007 - 11:09 PM
Post #2

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 122



Thanked: 3 times
Dream Kudos: 150
My Contributions
You can use the date struct present in dos.h

to store your date.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Strings
21 Nov, 2007 - 03:24 AM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 684



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Better still, use the C standard header for date and time, called <time.h>

http://www.cppreference.com/stddate/index.html

This post has been edited by Bench: 21 Nov, 2007 - 03:25 AM
User is offlineProfile CardPM
+Quote Post

nullonehalf
RE: Strings
22 Nov, 2007 - 09:10 AM
Post #4

New D.I.C Head
*

Joined: 7 Nov, 2007
Posts: 5


My Contributions
If you are just trying to parse user input from the keyboard or from a plain-text file and use that to determine the month, date and year, your best bet would be to use the built-in function strtok( ) in <string.h>. You can read about how to invoke the function on the Unix manual page for strtok if you are running a Unix terminal, or from the C standard reference at:

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/

What strtok( ) will do is break the string up into pieces according to some chosen set of delimiters (in your case, you would probably use ' ' and ',' as your delimiters), and with each successive call return a pointer to the next delimited token. If you know the order in which the tokens should appear, then you can call strtok( ) a fixed number of times, associating a meaning to each call.

In your case:

CODE

#define INPUT_MAX 255

...

char *inputstr, *daystr, *monthstr, *yearstr;

...

/*get input string from standard input*/

inputstr = (char*) malloc(INPUT_MAX * sizeof(char));
fgets(inputstr, INPUT_MAX, stdin);

...

/*parse input string and set pointers to expected fields*/

daystr = strtok(inputstr, ", ");
monthstr = strtok(NULL, ", ");
yearstr = strtok(NULL, ", ");


What you need to be aware of when using strtok( ) is that the string passed as the first argument will be changed; in particular, every occurrence of a character in the second argument will be overwritten by '\0'. If you still need to work with the full string, you should make a copy before calling strtok( ) on it.

Hope this helps.

'\0' .5

This post has been edited by nullonehalf: 22 Nov, 2007 - 09:12 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month