-
problem with time
Hello all...I am still very new to programming. I need to output the date in this form ##/##/####
I have tried to do my homework first, but I guess to be honest I just don't understand it well enought. I have <time.h> included.... and i found a reference that says the structure tm includes tm_day, tm_mon, tm_year...which I think is what I need but I don't know how to get it in my code. Can you help me please....I have read a ton of stuff...I can get it to output in string form, but not numerical form...i just need an example to follow.
Thankyou so much,
bigb1999
-
You're over complicating it, validate the format of the input and then validate the values. Read the line from the user, verify that there are two integer values, a / character, two more integer values, another / character, and four more integers.
Once you know the formatting is correct, then check the actual values of the integers. The first can't go higher than 12, the second can't go higher than 31, and the third can't go wherever you want your falloff year to be. This is all if..else statments and can be improved by testing what month it is and then checking that the day doesn't exceed the bounds of that month.
-Prelude
-
I need it to read time from the system...not output the date that the user has entered. I need to use some structure of time, but I am not sure exactly how to get it in the format that I stated above. I can get it to output the system time in the string format...but not ##/##/####....
anymore help would be great...
bigb1999
-
Get the time with time(), use gmtime() or localtime() to convert the system time into an instance of the struct tm structure. You can then use those values to format the output however you want. This page should help.
-Prelude
-
Thanks for all of your help.... this is what I have so far...
I have been successful enough to get that far...but how do i get a (for example) 0 to preceed the 3, since it is the third month... or get a 0 to proceed the day of the month if it was under 10.... any direction there...
#include <stdio.h>
#include <time.h>
#include <iostream.h>
struct tm* timetm()
{
time_t now;
time(&now);
struct tm* today = localtime(&now);
return today;
}
int main()
{
struct tm* now = timetm();
char stime [64];
sprintf (
stime,
"%d/%d/%d\n",
now->tm_mon+1,
now->tm_mday,
now->tm_year+1900
);
}
-
Fun with printf:
Code:
#include <cstdio>
int main ( void )
{
int x = 1, y = 10;
printf ( "Fun with printf: %0*d\n", ( x < 10 ) ? 2 : 1, x );
printf ( "Fun with printf: %0*d\n", ( y < 10 ) ? 2 : 1, y );
return 0;
}
-Prelude