Jump to content

Date Program


Recommended Posts

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 */

Link to comment
Share on other sites

	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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...