#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep 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();
    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    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.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    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
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    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.

IMN logo majestic logo threadwatch logo seochat tools logo