Thread: Formatting a string

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    Formatting a string

    OK, so this kinda goes along with the other post i made. In my app i'm storing the dates as MMDD in a deque <int> but i would like to output the date to the console as MM/DD what would be the best way to do this.
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    So you're storing the month & day together in one int between 0101 & 1231?
    Well I'd say the easiest way would be to convert the int into a string, then stick a '/' in the middle. You'll have to make sure the int gets a leading 0 when it converts to a string if the month is < 10.

  3. #3
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Well i'm storing it as an int because i'm sorting it by date. e.g. 612 > 610, so it goes before it in the list. So i'd be making life hard if i used it as a string. Unless i make a temporary string, assign it the value held in the int then stick the '/' in it and use that to print to the screen...
    Sorry, but i'm a Code::Blocks man now.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Terran View Post
    Well i'm storing it as an int because i'm sorting it by date. e.g. 612 > 610, so it goes before it in the list. So i'd be making life hard if i used it as a string. Unless i make a temporary string, assign it the value held in the int then stick the '/' in it and use that to print to the screen...
    I'm pretty sure that's what cp meant.

    Also: If you remember to put the zeroes in the string, the string will also sort in date order as well.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can sort strings. You can also store the month and day separately in a struct or class and sort by the class. You can overload the operator<< for the class and have it output the date in whatever format you want.

  6. #6
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by Daved View Post
    You can sort strings. You can also store the month and day separately in a struct or class and sort by the class. You can overload the operator<< for the class and have it output the date in whatever format you want.
    Interesting idea, although a bit much for this application.
    Sorry, but i'm a Code::Blocks man now.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Interesting idea, although a bit much for this application.
    Perhaps. It might also be less code than what you've got now, and it's the "right" way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM