Hello,
I have a question on how to represent day, month and year in C in the structure. I know a month must be a string. I am not sure where to start. I just need some kind of directions....
Thank you in advance!
Printable View
Hello,
I have a question on how to represent day, month and year in C in the structure. I know a month must be a string. I am not sure where to start. I just need some kind of directions....
Thank you in advance!
Code:enum Months {Jan, Feb, Mar, ..., Dec}
struct Date
{
Months month;
char Day;
char Year;
};
Representing the year in unnecessarily small datatype is what caused the Y2K problems, Vart. :rolleyes:
ok - __int64 can be used... in this case the program will survive till the end of the Galaxy (I suppose) ;)
from looking at that code its hard to figure out what you want to do.
heres some code that does somthing I guess:
hopefully its remotly close to what your trying to accomplish.Code:#include <stdio.h>
#include <string.h>
typedef struct {
int day;
char month[10];
int year;
} date;
int main(void) {
date today;
today.day = 30;
today.year = 2006;
strcpy(today.month,"November");
printf("Today's date is %s %d %d\n", today.month, today.day, today.year);
return 0;
}
Thank you for help...
It might be a stupid question, but will this work out with any date?
No, what sl4nted supplied will only work for today. And I think what he's pointing out is that what you supplied gives us no indication as to what you're trying to do...
all this does is print the values in the structure, which I supplied
does it work with any date?Code:today.day = 30;
today.year = 2006;
strcpy(today.month,"November");
if you mean can you change those values to any date, yes you should be able to.
the month can be 10 characters long, since I was too lazy to figure out which month has the most letters in it. And actually count how many. So as long as no month has more than 9 letters it should be fine.
I think he was wondering if what you'd supplied would work for any date. And it won't. Regardless of the date, it'll always print out : "Today's date is November 20 2006".
Yeah Happy_Reaper that is what I meant...
Is there a way the program would print any date? You guys are awesome!
Thank you for help...
Yes. Look at time.h
How about if the program just captures data(Prompts the user to enter any date) and outputs it by representing a date( day, month and year...)
Thanks a lot!
you should add tests of the input recieved, and read the faq for a better method of getting a string. this should get you going though, I'm lazyCode:#include <stdio.h>
#include <string.h>
typedef struct {
int day;
char month[10];
int year;
} date;
int main(void) {
date today;
printf("Enter the day> ");
scanf("%d", &today.day);
printf("Enter the month> ");
scanf("%s", today.month);
printf("Enter the year> ");
scanf("%d", &today.year);
printf("Today's date is %s %d %d\n", today.month, today.day, today.year);
return 0;
}
output:
Enter the day> 30
Enter the month> November
Enter the year> 2006
Today's date is November 30 2006
Thank you!
Yes, it seems the better way of using string is the only thing I will need!
Even using scanf for integer input could lead to problems. Usually, a combination of fgets and sscanf is what you'd want.
And sl4nted, usually the policy around here is not to do other people's code for them, especially when they have yet to post an attempt themselves.