The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C++ break date into string and show full words
Discuss C++ break date into string and show full words in the C Programming forum on Dev Shed. C++ break date into string and show full words C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 9th, 2012, 08:01 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 2
Time spent in forums: 1 h 53 m 38 sec
Reputation Power: 0
|
|
|
C++ break date into string and show full words
I am stuck, I do not know where to begin. I have to take this code and make it print the date where it shows, the name of the day and month. I am thinking about using a switch to determine which day of the week and month it is but not sure where to begin to break this string apart. I need to do this by searching for spaces and then saving each part (day, date, month, year,) as a substring and then change them into the full name for each. The problem I am having is getting started, lol, I can change the parts to their full name easy enough, just not sure how to get the parts saved into the substrings. Any help to get me started would be greatly appreciated.
Code:
#include<iostream>
#include<ctime>
using namespace std;
void main ()
{
time_t now = time(0);
cout << ctime(&now);
cin.get();
}
|

October 9th, 2012, 11:16 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Investigate the functions in ctime, which just #includes C's time.h so go straight to time.h.
time_t contains the number of seconds since the midnight starting 01 Jan 1970. There's a struct tm structure with fields for all day, weekday, month, year, day of year, hours, minutes, sec, etc. There are also two functions to convert a time_t to a struct tm and a function to convert a struct tm to a time_t. There's also a function that will create a string formatted as you specify in a format string. And much more.
Investigate the functions in time.h .
Last edited by dwise1_aol : October 9th, 2012 at 11:19 PM.
|

October 10th, 2012, 12:49 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
In particular, you might want to investigate localtime() and strftime(). You can use strftime() to print the different parts of the date separately.
Another way to do this is to use ctime() to generate a string. Since you're using C++, assign it to a std::string and the use the substring() method to pull out the substrings.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

October 10th, 2012, 01:44 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
I must have only used a little more than half of what time.h has to offer, which didn't include ctime(). That comment was meant to convey how rich in resources that header file is.
Though whouldn't ctime() still give the OP a C-style string? So if he's using C++'s basic string class, he'll have to be mindful of how to handle the two different kinds of strings.
For the OP's edification:
In the first decade of its existence, C++ continued to use C-style strings. It wasn't until 1998 that they added the basic string class to the language. As a result, whenever you use a C function that expects or returns a string, it will be a C-style string and not a string object -- for passing a string object as a C-style string, use the c_str() method. Also, there are methods in iostreams that still require a C-style string and will not accept a string object.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|