My problem states: Write a C function named days() that determines the number of days from the date 1/1/1900 for any date passed as a structure.Pass the address of a Date structure variable to the days() function. Write a program in main() that inputs the month, day, and year from the user, writing the inputs into a Date structure variable, call your days() function and display the result. Use the following date structure:
struct date
{
int month;
int day;
int year;
};
In writing this function, use the convention that all years are 360 days and each month consist of 30 days. The function should return the number of days for any date structure passed to it.
This is what i have so far, and it is calculating 0 each time:
struct date
{
int month;
int day;
int year;
};
int main()
{
int monthMain, dayMain, yearMain; //declaring the int variables
int totalDays;
printf("Enter a Month: "); //requesting user to input the month
scanf("%d", &monthMain); //accepting the user input for month
printf("Enter a Day: "); //requesting user to input the day
scanf("%d", &dayMain); //accepting the user input for day
printf("Enter a Year: "); //requesting user to input the year
scanf("%d", &yearMain); //accepting the user input for year
totalDays = days();
printf("the date you entered = %d days", totalDays);
return 0;
}
int days(struct date *d)
{
int yearCalc, daysAmount;
int monthMain, dayMain, yearMain; //declaring the int variables
yearCalc = 1900 * 360;
yearMain = (yearMain * 360) - yearCalc;
if(monthMain == 1)
{
monthMain = 0;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 2)
{
monthMain = 30;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 3)
{
monthMain = 60;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 4)
{
monthMain = 90;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 5)
{
monthMain = 120;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 6)
{
monthMain = 150;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 7)
{
monthMain = 180;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 8)
{
monthMain = 210;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 9)
{
monthMain = 240;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 10)
{
monthMain = 270;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 11)
{
monthMain = 300;
daysAmount = monthMain + dayMain + yearMain;
}
if(monthMain == 12)
{
monthMain = 360;
daysAmount = monthMain + dayMain + yearMain;
}
return daysAmount;
}
Any help would be great :)
daysAmount = monthMain + dayMain + yearMain;is the same each time, you could move that after the set ofifstatements. The jump from 300 to 360 between month 11 and 12 is wrong. There's a simple algorithm given a valid month number for deducing the number number of days in the year prior to that (for these nice simple uniformly-sized months):(month_number - 1) * 30gives the answer you're after. Have you added any printing statements to see why you're getting 0?