Question

Locked

HELP! Birthday Calculator, C language

By remarkable.rocket ·
Tags: Off Topic
I have just started programming and I am VERY CONFUSED about this assignment. For some reason my code will only print Wednesday, no matter WHAT day or YEAR I enter. Yet, I feel like it should work just fine! What is going on?
Basically, it needs to ask the user for a date, and it will tell the user the weekday of that date. PLEASE HELP, I am in danger of failing this class and I DON'T KNOW WHAT TO DO!


#include <stdio.h>
#include <math.h>
#include <time.h>


int calculateDateDay (int, int, int);
void printWeekday (int, int, int);
void printMonth (int);
void printDateDay(int, int, int);

int main ()
{
int day;
int month;
int year;
int weekday;

printf("Enter a day of month (1-31):");
scanf("%d", &day);
printf("Enter a month (1-12):");
scanf("%d", &month);
printf("Enter a year:");
scanf("%d", &year);



printDateDay(day, month, year);

}

void printMonth (int month)
{

switch(month)
{
case 1: printf( "January" );
break;
case 2: printf( "February" );
break;
case 3: printf( "March" );
break;
case 4: printf( "April" );
break;
case 5: printf( "May" );
break;
case 6: printf( "June" );
break;
case 7: printf( "July" );
break;
case 8: printf( "August" );
break;
case 9: printf( "September" );
break;
case 10: printf( "October" );
break;
case 11: printf( "November" );
break;
case 12: printf( "December" );
break;
}
}


int calculateDateDay (int month, int day, int year)
{

int dec31 = ((year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 ) % 7;



switch(month)
{
case 12: day + 30;
case 11: day + 31;
case 10: day + 30;
case 9: day + 31;
case 8: day + 31;
case 7: day + 30;
case 6: day + 31;
case 5: day + 30;
case 4: day + 31;
case 3: day + 28;
case 2: day + 31;
case 1: day + 0 + dec31;
}


}


void printWeekday (int month, int day, int year)
{

int weekday = (calculateDateDay(month, day, year)) % 7;

switch(weekday)
{
case 0: printf("Sunday");
break;
case 1: printf("Monday");
break;
case 2: printf("Tuesday");
break;
case 3: printf("Wednesday");
break;
case 4: printf("Thursday");
break;
case 5: printf("Friday");
break;
case 6: printf("Saturday");
break;
}
}


void printDateDay (int day, int month, int year)
{

printMonth(month);
printf(" %d, ", day);
printf("%d, is a ", year);
printWeekday(month, day, year);

}

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Well whatever else may not be exactly right

by Tony Hopkinson In reply to HELP! Birthday Calculator ...

I'd recommend having CalculateDateDay return a value. e.g

return day;

as the last statement in the function.

Remember you are passing in the value of day, not a reference to where day is stored.
So day becomes a local variable to the function and any changes to it are binned as soon as the function returns.

Collapse -

Pointer.

by normhaga In reply to HELP! Birthday Calculator ...

There are several problems. The reason you print the same day is that you are not calling a function that actually calculates the day. It would be helpful to you and others if you documented your code.

Look at your main function:

int main ()
{
int day;
int month;
int year;
int weekday;

printf("Enter a day of month (1-31):");
scanf("%d", &day);
printf("Enter a month (1-12):");
scanf("%d", &month);
printf("Enter a year:");
scanf("%d", &year);


printDateDay(day, month, year);
}

You now have to look at the function "printDayDate" because it is the only function you are calling:

void printDateDay (int day, int month, int year)
{

printMonth(month);
printf(" %d, ", day);
printf("%d, is a ", year);
printWeekday(month, day, year);

}

printMonth is ok although I would add some bounds checking; what happens if a user enters a number < 1 or > 12?

Now on to printWeekday:

void printWeekday (int month, int day, int year)
{

int weekday = (calculateDateDay(month, day, year)) % 7;

switch(weekday)
{
case 0: printf("Sunday");
break;
case 1: printf("Monday");
break;
case 2: printf("Tuesday");
break;
case 3: printf("Wednesday");
break;
case 4: printf("Thursday");
break;
case 5: printf("Friday");
break;
case 6: printf("Saturday");
break;
}
}

What are you doing as the first function? Calling calculateDateDay, so look there:


int calculateDateDay (int month, int day, int year)
{

int dec31 = ((year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 ) % 7;


switch(month)
{
case 12: day + 30;
case 11: day + 31;
case 10: day + 30;
case 9: day + 31;
case 8: day + 31;
case 7: day + 30;
case 6: day + 31;
case 5: day + 30;
case 4: day + 31;
case 3: day + 28;
case 2: day + 31;
case 1: day + 0 + dec31;
}
}

Look at the function 'dec31'.

I do not know just what you think you are doing, but look closely at that formula.

Lets plug a few numbers in now: oh say 10/31/2008.

s0 == 2007 * 365;
s1 == 2007 / 4;
s2 == 2007 / 100;
s3 == 2007 / 400
dec31 == s0 + s1 -s2 + s3 % 7;
dec31 == 732555 + 733056 - 20 / 5; == 293,118.2
dec31 == dec31 % 7 == 2;

Do you see the problem? Try it with a few other numbers. This is called a walkthrough.

Your switch statement also has a problem.

Also, look closely at your syntax.

We here at techrepublic do not do homework. In your case I made an exception. Your instructor and fellow students are much better resources.

Collapse -

Sheesh I've been spoilt by C#

by Tony Hopkinson In reply to Pointer.

never even noticed the switch statement, compiler blows chunks on that one immediately.

If I'm not careful, I'm going to atrophy into a cookie cutter....

Back to After Hours Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums