Problem with dates!!

Hi all,

For a portion of my code, I need to either input a date from user or capture the system date. From there onwards I have to add 7 more days to the actual date and store it as the variable 'DueDate'. The format for the stored DueDate is DDMMYY. I am very confused as I am facing a lot of problems.

System time, seems very confusing and I cant get it to work, so I decided to stick to the traditional - "Let the user input the date" method.

Problems I need to tackle:
1. Output the DueDate in the correct format. Even if I meddle around with the input values as integers, I cant output in the correct format, for example - if the user enters "1" for month, I can not display it as "01" (and i need it to be 01 since that is the required format). How can i do that?

2. The logic I have applied for the date (like, incrementing month, if the due date exceeds number of days in month, or incrementing the year if month in duedate exceeds number of months in a year) is very troublesome.

Please help.

I wouldn't mind altering my code to use system time if it is less troublesome. I prefer neat coding in my programs.

Thank you.
1) setw. See example on this page: http://cplusplus.com/reference/iostream/manipulators/setw/


2) There's no real straightforward way to do this since each month has a different number of days.

Best you can really do is make a lookup table to indicate how many days are in each month so you avoid a huge if/else chain

 
const int days_per_month[12] = {31,28,31,30,31, ... };
Thanks for the quick reply, Disch! Appreciate it. :)

1. But how do I set an integer in place of a blank space. I mean if i setw(2) then it'll output as "_7". How can I set the blank as an integer like 0, which will then solve my problem.

2. Yeah. I will consider the lookup table idea. Thanks.

Thanks again,
Cyberbeast
Also, I realised that I need to sort the dates. in descending order (latest date first)
Any suggestions for methods on doing this?
for
1. I can use setfill(). yipee! :)

could u still suggest a method on how i can sort the dates?

thanks.
Topic archived. No new replies allowed.