October 9th, 2012, 09:01 PM
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.
using namespace std;
void main ()
time_t now = time(0);
cout << ctime(&now);
October 10th, 2012, 12:16 AM
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 10th, 2012 at 12:19 AM.
October 10th, 2012, 01:49 PM
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
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
October 10th, 2012, 02:44 PM
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.