I have a programming assignment and we are asked to create a program in which the user will be able to enter the date in this format dd/mm/year. Then we have to find a way to extract each of those numbers and put in the day, month and year fields in a structure.
I thought to read the whole input of the user as a string and then just select the values of the string that I want. Here is my code.
The structure declaration:
struct datestructure{
char day;
char month;
char year;
};
struct prj{
int ID;
struct namestructure name;
struct datestructure date;
float price;
};
struct prj project[MAX];
And here is the code that I have written.
char dateinput[11];
scanf("%s", dateinput);
printf("%s", dateinput); //making sure that the input is read correctly
project[n].date.day = dateinput[0]+dateinput[1];
printf("%s", project[n].date.day );
However this is not working and I am running out of ideas. Could you give me some guidance on how to solve this problem.
Thanks.
Here are some suggestions:
Some basic sample for parsing the string (Just an example for you to understand. There are other better methods also):
Note: The above code doesn’t take care of erroneous input and considers that day and month having single digits will be enetered with a preceding 0. Ex Input: 07/03/2012. Note the preceding 0 before 7 and 3. First you can try like this and later on improvise by yourself to handle all error scenarios.