Peter_APIIT Posted March 22, 2007 Report Share Posted March 22, 2007 Hello all of you, i totally a newbie to programming,linux and computer also. I wrote a program which calulate the days between the initlized date and date enter by user. After calculated, rreturn the days of week. Unfortunately, the calulation is not working. Any idea or recommendations ? Your help is greatly appreciated by me and others. /* * File: Date.c * Author: nicholas_tse * * Created on March 18, 2007, 7:56 PM */ #include <stdio.h> #include <stdlib.h> #include "Date.h" int Days_Of_Week(const struct U_Days *, const struct U_Months *, const struct U_Years *); int main(int argc, char** argv[]) { static struct Days days = {1}; static struct Months months = {1}; static struct Years years = {2002}; // --------------------------------------------------- char *Date[7]= {"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday" }; // --------------------------------------------------- int result; struct U_Days *days_ptr; struct U_Months *months_ptr; struct U_Years *years_ptr; days_ptr = &u_days; months_ptr = &u_months; years_ptr = &u_years; // -------------------------------------------------- do { printf("Enter a day in integer: "); scanf("%d", &days_ptr->days); printf("Enter a month in integer: "); scanf("%d", &months_ptr->months); printf("Enter a year in integer: "); scanf("%d", &years_ptr->years); result = Days_Of_Week(days_ptr, months_ptr, years_ptr); printf("\nThe date your enter equal to days of week is %s\n\n", Date[result]); }while(days_ptr->days >0 && days_ptr->days <32 || months_ptr->months >0 && months_ptr->months <13); return (EXIT_SUCCESS); } // ----------------------------------------------------- int Days_Of_Week(const struct U_Days *days_ptr, const struct U_Months *months_ptr, const struct U_Years *years_ptr) { static struct Days days = {1}; static struct Months months = {1}; static struct Years years = {2002}; // ----------------------------------------------------- char *Date[7] = {"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "Monday" }; int days_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int loop; int temp_days; int k; // ------------------------------------------------------ if (years_ptr->years % 4 ==0) { days_of_month[1] = 29; } else { days_of_month[1] = 28; } for (k=0; days_of_month[k]<12; k++) { temp_days = temp_days + days_of_month[k]; } // Why here the the loop won't calculate the total days; printf("\n%d", days_of_month[3]); printf("\nTotal of days is %d", temp_days); if (days_ptr->days % 7 == 1) { return 0; } else if (days_ptr->days % 7 == 2) { return 1; } else if (days_ptr->days % 7 == 3) { return 2; } else if (days_ptr->days % 7 == 4) { return 3; } else if (days_ptr->days % 7 == 5) { return 4; } else if (days_ptr->days % 7 == 6) { return 5; } else if (days_ptr->days % 7 == 0) { return 6; } } /* * File: Date.h * Author: nicholas_tse * * Created on March 18, 2007, 9:27 PM */ #ifndef _Date_H #define _Date_H struct U_Days { int days; }u_days; struct U_Months { int months; }u_months; struct U_Years { int years; }u_years; // ---------------------------------------------------- static struct Days { int days; }days; static struct Months { int months; }months; static struct Years { int years; }years; #endif /* _Date_H */ Quote Link to comment Share on other sites More sharing options...
neddie Posted March 22, 2007 Report Share Posted March 22, 2007 for (k=0; days_of_month[k]<12; k++) { temp_days = temp_days + days_of_month[k]; } I'm not quite sure what you're trying to do in this section, but I can see that this loop is wrong. It says "loop through the months as long as the number of days in the month is less than 12". So of course it jumps out of the loop straight away. But then you don't seem to be using temp_days after that anyway. And I think your big if..else if... else if statement below that can be replaced by a simple (days-1)%7. Quote Link to comment Share on other sites More sharing options...
Peter_APIIT Posted March 23, 2007 Author Report Share Posted March 23, 2007 Actually, what i want to do is i would like to calculate the days between two date. A date enter by user and a initialized date. calculate the different between two date and divide % 7 and return the remainder, determine the days of week such as monday or sunday. Quote Link to comment Share on other sites More sharing options...
neddie Posted March 23, 2007 Report Share Posted March 23, 2007 ok, then that loop is definitely wrong. Think about it, what is k? If it's the month number then call it something like month_number. And why are you looping until the number of days in the month is at least 12? I think instead that you want to loop from the first date (your "initialized date"?) up to the second date (the one entered by the user). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.