Reputation: 17
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 :)
Upvotes: 1
Views: 5671
Reputation: 251
struct date
{
int month;
int day;
int year;
}; // unsigned int would serve this purpose better, as negative values are not valid here
int days(struct date *d)
{
int daysTotal; // Declaring variable to hold total number of days since 1900-01-01
// Adding days from date::day
if (d->day > 0 && d->day < 31){ // Only execute if day is valid
daysTotal = d->day - 1;
}
// Adding days from date::month
if (d->month > 0 && d->month < 13){ // Only execute if month is valid
daysTotal += 30 * (d->month-1);
}
// Adding days from date::year
if (d->year > 1899){ // Only execute if year is valid
daysTotal += 360 * (d->year-1900); // subtracting 1900 years
}
return daysTotal; // This may still be a negative value, but is accepted here for simplicity
};
int main()
{
date myDate; // Declaring an object of struct date
printf("Enter a Month: "); //requesting user to input the month
scanf("%d", &myDate.month); //recieving the user input for month
printf("Enter a Day: "); //requesting user to input the day
scanf("%d", &myDate.day); //recieving the user input for day
printf("Enter a Year: "); //requesting user to input the year
scanf("%d", &myDate.year); //recieving the user input for year
printf("the date you entered = %d days", days(&myDate)); // Passing object pointer to function
return 0;
};
You didn't initially use the structure at all. You didn't make an object of the structure for that purpose. There was a big chunk at the end that I could collapse into an if statement. Other struct members didn't have validation applied. There were unnecessary variables. I made changes to simplify things. Put the days() function before main(), to prevent "function days(struct date *d) not declared in this scope" error.
EDIT: My implementation now returns 0 days for 1900-01-01
and 31 days for 1900-02-02
. This is done by subtracting 1 from day and month, to convert it to the index, rather than the amount of passed units, as this is the actual meaning of days and months (not so with years, they begin with year 0 or here, with year 1900).
Upvotes: 1
Reputation: 753725
Your main problem is this call:
totalDays = days();
You need to pass a pointer to a date structure to the function. That means you need a local variable in main()
:
struct date given;
and you need to initialize it:
given.day = 11;
given.month = 11;
given.year = 1918;
and you need to pass it:
totalDays = days(&given);
and you need to learn how to set the compiler warnings so that you get error messages from your compiler when you don't pre-declare functions with full prototypes and don't call functions accurately.
If your compiler is GCC, use:
gcc -O3 -std=c11 -g -Wall -Wextra -Werror …
as a starting point. You might need -Wstrict-prototypes
or -Wold-style-declaration
or -Wold-style-definition
or -Wmissing-prototypes
too; I use most of those as routine to make sure I'm not making silly mistakes.
Additionally, since the calculation daysAmount = monthMain + dayMain + yearMain;
is the same each time, you could move that after the set of if statements. 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) * 30
gives the answer you're after.
Have you added any printing statements to see why you're getting 0? For example, if you'd added a statement at the start of the function like:
printf("Date: %.4d-%.2d-%.2d\n", d->year, d->month, d->day);
you'd be printing the date in ISO 8601 format. If this didn't show you the answer you thought you'd entered, you'd know that there's a problem. Printing values to make sure what the computer sees is what you thought the computer should see is a basic debugging technique.
Upvotes: 3